This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes how does Set understands duplicate elements? 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 » Java » Beginning Java
Bookmark "how does Set understands duplicate elements?" Watch "how does Set understands duplicate elements?" New topic
Author

how does Set understands duplicate elements?

amit daundkar
Ranch Hand

Joined: Jan 24, 2009
Posts: 47
How does Set understand if duplicate element is inserted? I mean does it perform "equals" test everytime an object is inserted?

Please reply.
Thanks.
Viktor Kubinec
Ranch Hand

Joined: Jan 28, 2012
Posts: 34
In order to understand this, you should first understand hashCode and equals contract.

Find HashSet on this site :
Set interface

If hashCodes equal and objects equal, then the insertion is resolved as duplicated element and the add method returns false.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18545
    
  40


It''s also implementation specific. For example, the TreeSet uses either the Comparable or Comparator interfaces.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
amit daundkar
Ranch Hand

Joined: Jan 24, 2009
Posts: 47
Thank you Viktor, Henry.
Hemant B. Kumar
Greenhorn

Joined: Nov 22, 2011
Posts: 11

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?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18545
    
  40

Hemant B. Kumar wrote:
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.

Henry
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

If it isn't you can get odd behaviour. For instance:
What you now get is that set3.equals(set1) but !set1.equals(set3).


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
 
subject: how does Set understands duplicate elements?
 
Similar Threads
Avoid submission of form multiple times
Marcus Green Q=21 Collection
problems about Collections
Set duplication
methods, instances and arrays - swimming in the ocean of java