wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes generics Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "generics" Watch "generics" New topic
Author

generics

swaraj gupta
Ranch Hand

Joined: Oct 22, 2010
Posts: 181

And if I use it as:
Why exactly one element is getting added to the TreeSet if both the objects d1 and d2 are different....
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

return 0; implies equality

When 2 objects are equal, the duplicate one will not make it into the set


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
swaraj gupta
Ranch Hand

Joined: Oct 22, 2010
Posts: 181

Deepak Bala wrote:return 0; implies equality

When 2 objects are equal, the duplicate one will not make it into the set

doesn't TreeSet uses equals() to test equality of two objects...?

where I can read about this?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18546
    
  40

swaraj gupta wrote:doesn't TreeSet uses equals() to test equality of two objects...?

where I can read about this?


No. And the JavaDoc for TreeSet states this in one of the first few paragraphs. The JavaDoc further states that the comparator/comparable must also be "consistent with equals", meaning it will assume that the comparable/comparator methods can be used instead of equals -- no further checking is done.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: generics
 
Similar Threads
K&B chapter 7 question 15. Collections.
Problem faced in using hashset(comparison of equal elements)
Doubt in TreeSet
HashMap
is this correct way of implimenting comparable interface