Henry Wong wrote:
It''s also implementation specific. For example, the TreeSet uses either the Comparable or Comparator interfaces.
Yeah that's right but I think in that case also it would be using equals and hashcode method, isn't it?
Interestingly, the answer is no. The reason it is interesting is because if you look at the JavaDoc for java.util.Set, it is actually defined as using the equals() method to determine duplicates. However, the java.util.TreeSet class does not use the method -- and the JavaDoc explicitly mentions that it is the programmers responsibility to guarantee that it is consistent -- meaning that if Comparable or Comparator says that two objects are equal, the equal() method should return equal.