Win a copy of AWS Security this week in the Cloud/Virtualization 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 ...
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
  • Piet Souris
  • salvin francis
  • fred rosenberger

Hasset and Hasmap difference

Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to use HasSet and HasMap and main difference
Posts: 6663
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.
Posts: 21955
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.
Won't you please? Please won't you be my neighbor? - Fred Rogers. Tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
    Bookmark Topic Watch Topic
  • New Topic