This week's book giveaway is in the Artificial Intelligence forum.
We're giving away four copies of Pragmatic AI and have Noah Gift on-line!
See this thread for details.
Win a copy of Pragmatic AI this week in the Artificial Intelligence 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:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

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: 59786
188
  • 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: 23649
49
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: 21426
94
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: 23649
49
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: 59786
188
  • 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: 59786
188
  • 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.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!