• Post Reply Bookmark Topic Watch Topic
  • New Topic

HashSet - hashing technique?  RSS feed

 
Ranch Hand
Posts: 1490
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whether HashSet uses hashing technique(equals and hashCode()) like HashMap/Hashtable for storing/retrieving elements?
 
Ranch Hand
Posts: 449
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

kri shan wrote:Whether HashSet uses hashing technique(equals and hashCode()) like HashMap/Hashtable for storing/retrieving elements?


Yes.
 
Marshal
Posts: 58823
179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your Java installation folder there is a file called src.zip. If you open that, and search (the folder names are the same as the parts of the package names between .s) you can find the source for HashSet and all your questions will be answered
 
kri shan
Ranch Hand
Posts: 1490
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HashSet is not using key/value pair, it has only value. How hash bucket value is calculated without key?
 
Sheriff
Posts: 23486
46
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It has a key but no value.
 
Sheriff
Posts: 21305
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually it does*, but the value is irrelevant. The current HashSet implementation is little more than a wrapper around a HashMap key set.

From the source:
That single object is the value for each key.
 
Rancher
Posts: 4686
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rob Prime wrote: The current HashSet implementation is little more than a wrapper around a HashMap key set..


This is also true for many alternative implementations, such as the ImmutableSet from the Google-Collections library. Most of the time, a HashSet is a HashMap, with the value either null or set to a constant dummy object.
 
kri shan
Ranch Hand
Posts: 1490
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As per Java API explanation:

add(Object o)
Adds the specified element to this set if it is not already present.


Object o is the key or value?
 
Paul Clapham
Sheriff
Posts: 23486
46
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. It's like the API document says, it's an element being added to a set. "Key" and "Value" are concepts for a map, not for a set. So, it's neither of them.
 
Campbell Ritchie
Marshal
Posts: 58823
179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Pat Farrell says, it might contain null, or a constant dummy object in the implementation. It might also contain any other type of Object, which might be the same, or different, for every key. All those would work, but different Objects would be wasteful of memory.
So Paul C is correct to say it does not behave as a Map with key and value, but as a Set with elements only.
 
Campbell Ritchie
Marshal
Posts: 58823
179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

kri shan wrote:add(Object o)

Which version of Java is that? For the last 5½ years, it has read add(E e) or similar.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!