I have created a Student class like this:
public class Student implements Comparable<Student> {
private String firstName;
private String lastName;
public Student(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
// Getters & Setters follow here...
@Override
public int compareTo(Student student) {
int hash = this.firstName.compareTo(student.firstName);
return hash;
}
@Override
public String toString() {
return "Student [firstName=" + firstName + ", lastName=" + lastName
+ "]";
}
}
This is my test class where I just add elements to my TreeSet:
public class SortedSetExample1 {
public static void main(String[] args) {
SortedSet<Student> set = new TreeSet<Student>();
set.add(new Student("A1","A2"));
set.add(new Student("B1","B2"));
set.add(new Student("A1","B2"));
set.add(new Student("A2","B2"));
System.out.println(set);
}
}
As per my program the output is:
[Student [firstName=A1, lastName=A2], Student [firstName=A2, lastName=B2], Student [firstName=B1, lastName=B2]]
In my test class I am adding Student
objects to TreeSet
, and also I have not overridden the hashCode
& equals
methods. So I was expecting that the TreeSet
will hold all the 4 objects but I can also see that it contains 3 objects. Can you please explain why new Student("A1","B2")
is not part of my TreeSet
?
Also as per the Java docs for TreeSet here, it says:
Adds the specified element to this set if it is not already present. More formally, adds the specified element e to this set if the set contains no element e2 such that (e==null ? e2==null : e.equals(e2)). If this set already contains the element, the call leaves the set unchanged and returns false.
As I have not overridden the equals
method then why the collection is not having all the four elements?
Comparator to return a descending order in Java TreeSet at the time of the creation of TreeSet instead of making a new class. Pseudo Code: Approach TreeSet<Integer> set = new TreeSet<Integer> (new Comparator<Integer> () { public int compare (Integer i1,Integer i2) { // comparing using compareTo () method return i2.compareTo (i1); } });
Java SortedSet and TreeSet Tutorial and Examples. In this tutorial, we help you understand deeper about TreeSet in the Java Collections Framework. You know, TreeSet does not only implement the Set interface, it also implements the SortedSet and NavigableSet interfaces. Therefore, besides inheriting behaviors of a typical Set, ...
It is recommended that compareTo only returns 0, if a call to equals on the same objects would return true:
The method returns 0 if the string is equal to the other string. A value less than 0 is returned if the string is less than the other string (less characters) and a value greater than 0 if the string is greater than the other string (more characters).
As java.util.TreeSet says:
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
Kudos to @Jon Skeet.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With