This week's book giveaway is in the General Computing forum.
We're giving away four copies of Emmy in the Key of Code and have Aimee Lucido on-line!
See this thread for details.
Win a copy of Emmy in the Key of Code this week in the General Computing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

How does set ensures uniqueness

 
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried looking code for Set and Hashset ,but from that i could not find out how set ensures duplicates elements are not present in it.Can someone tell.
 
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Generally the javadoc is easier to read than the code.

add

public boolean add(E e)

Adds the specified element to this set if it is not already present. More formally, adds the specified element e to this set if this 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.



That's from HashSet.
Because HashSet allows a null element, it checks if there's already a null element if the new one is null. If it's not null, it uses the equals() method found in Object. That implementation returns true if the 2 references are the same and typically a class would override this method and check if the 2 instances are meaningfully equal.

Say your class SomeObject has a String or 2, it might compare each of those Strings using String's .equals() implementation.
 
Marshal
Posts: 66160
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HashSet uses a Map implementation, which I think is a HashMap; it inserts your value as the "K" in the Map and a "dummy" Object instance as the "V"; I think it is the same "V" throughout.
All you have to do in this instance is go through the Map and see whether it already contains the "K". If it hasn't, add it and return true; if it's already there return false.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And the relevant code from HashMap.put is this (as of JDK 1.6):

 
Campbell Ritchie
Marshal
Posts: 66160
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Ilja Preuss:
And the relevant code from HashMap.put is this . . .

You can see it doesn't alter the key; in the case of a HashSet, the value is always a reference to the same "dummy" Object, so that doesn't actually change either. Its correct operation is however dependent on the equals() and hashCode() methods being overridden correctly.
 
pie. tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!