• 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

Hasset and Hasmap difference

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to use HasSet and HasMap and main difference
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kumaran,

Welcome to javaranch. This question has been asked here before. In fact if you do a google search - "hashset vs hashmap" it will link you to several discussions here. You can also use the search feature in this forum to search for previous posts.
 
Sheriff
Posts: 21817
104
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A set is a (flat) collection with unique elements. A map is a dictionary; a mapping from keys to values. You can't really compare the two.

And then I contradict myself; you can most definitely compare them. The reason you can is because, at present, a HashSet actually uses a HashMap. It basically delegates its storage to a HashMap - the keys are the Set elements, and the values are irrelevant. The following are taken from the HashSet implementation:
Now they use one single object for all values, but like I said, it's irrelevant. By ensuring that no element has a null value, add and remove could have been written as this:
Using a static object is more efficient though.


So if you would ask what the difference is between a HashSet and the keySet() of a HashMap(), the answer would be: just about nothing.
 
He's dead Jim. Grab his tricorder. I'll get his wallet and this 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!