TOTOROCATBUS
TOTOROCATBUS

Reputation: 181

Sorting tree sets without removing duplicates

myData{
itemId,
location,
ExpDtTm,
userId
}

I trying to sort a TreeSet like so:

 TreeSet<myData> sorted = new TreeSet<>(Comparator.comparing(myData -> myData.ExpDtTm));

But there is a problem if any of the dates are equivalent they wont be added to the tree set which proves to be a problem. If anyone knows what to do help is much appreciated.

I also tried

TreeSet<myData> sorted = new TreeSet<>(Comparator.comparing(myData -> myData.ExpDtTm).thencomparing(myData -> myData.itemId));

But it stopped excepting myData in both of the lambdas.

Upvotes: 2

Views: 1536

Answers (4)

Harvansh Singh
Harvansh Singh

Reputation: 29

Employee.Java

private String name;

public Employee(String name) {
    this.name = name;
}
public void setName(String name) {
    this.name = name;
}

public String getName() {
    return this.name;
}

public String toString() {
    return "Employee[ id=" + name + "]";
}

Driver Class

TreeSetTest.Java

    TreeSet<Employee> treeSet = new TreeSet<>(new Comparator<Employee>() {
        @Override
        public int compare(Employee o1, Employee o2) {
            int index = o1.getName().compareTo(o2.getName());
            return index==0?1:index;
        }
    });
    treeSet.add(new Employee("Harvansh"));
    treeSet.add(new Employee("Harvansh1"));
    treeSet.add(new Employee("Harvansh"));
    treeSet.add(new Employee("Harvansh2"));
    treeSet.add(new Employee("Harvansh"));
    treeSet.add(new Employee("Harvansh3"));
    treeSet.add(new Employee("Harvansh"));
    treeSet.forEach(System.out::println);

Output

Employee[ id=Harvansh]

Employee[ id=Harvansh]

Employee[ id=Harvansh]

Employee[ id=Harvansh]

Employee[ id=Harvansh1]

Employee[ id=Harvansh2]

Employee[ id=Harvansh3]

Here Is The Complete Solution With Output

Upvotes: 2

TOTOROCATBUS
TOTOROCATBUS

Reputation: 181

This is what I ended up doing:

final SortedSet<myData> sorted = new TreeSet<>(new Comparator<myData>() {
            @Override
            public int compare(myData o1, myData o2) {
                 int comparedValue = o1.expDtTm.compareTo(o2.expDtTm);
                 if(comparedValue == 0){
                       comparedValue = 1;
                 }
            }
});

Inside of that comparedValue if statement you can add any other variables you might want to sort with as well.

Upvotes: 1

Mureinik
Mureinik

Reputation: 311228

The reason this happens is because the comparator is used to not only to determine the order but also to determine whether the two objects are equal (instead of hashCode, which a HashSet would use):

a TreeSet instance performs all element comparisons using its compareTo (or compare) method, so two elements that are deemed equal by this method are, from the standpoint of the set, equal.

If you have two objects that aren't, in fact, identical but just happen to have the same date, you could use the object's identity hash code (effectively, it's memory address) as a secondary sorting condition:

TreeSet<myData> sorted = 
    new TreeSet<>(Comparator.comparing(myData -> myData.ExpDtTm)
                            .thenComparingInt(System::identityHashCode));

Upvotes: 2

user31601
user31601

Reputation: 2610

A TreeSet is an implementation of Set, which explicitly forbids duplicate entries by design. If you want a sorted collection that includes duplicates, use an implementation of List (e.g. ArrayList), and sort it at the end by calling Collections.sort(myList, Comparator.comparing(myData -> myData.ExpDtTm));.

Upvotes: 1

Related Questions