This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
The output is: Are they equal: false Do they have the same hashcode: false 0 Is a equals to b: false Is b in the TreeSet: true 1 How could this be happening? a and b have are not equal, and they have different hashCode. why is b in the TreeSet after I added a only? It seems to me that the contains() method only looks at compareTo method, and return true if compareTo returns 0. in the above if I change the compareTo method and return -1 (it is bad, used just for demonstration purpose). then s.contains(b) in the last line of main method will return false. Any idea? Thanks in advance. [ edited to break long line and to remove the evil tab character -ds ] [ April 27, 2004: Message edited by: Dirk Schreckmann ]
How about writing a proper compareTo method? If you're unsure about the proper operation of the compareTo method, perhaps you'd taking a look at the Comparable interface documentation, which describes how the compareTo method is supposed to work. [ April 27, 2004: Message edited by: Dirk Schreckmann ]