Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript 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
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Difference in HashMap and HashSet

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone explain how is HashMap different from HashSet? How does the contains() method of HashSet work?
Any useful link about the general concepts of collections would be highly appreciated.
 
Ranch Hand
Posts: 393
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HashSet allows us to store objects in the set where as HashMap allows us to store objects on the basis of key and value. Every object or stored object will be having key.

Contains :- method checks the hashcode value of the object, if the value found in already added list of objects in hash set ir returns true else false
 
Gyanesh Sharma
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply James. I guess my question is then if contains() method checks the hashCode value of an object, isn't it similar to the key and value concept that you explained about the HashMap?
How is the use of hashCode different on HashMap and HashSet?
 
Ranch Hand
Posts: 502
jQuery Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gyanesh,

Both hashset and hashmap use object hashcode to find the bucket(place where all the object with same hashcode are stored). Then uses equals method of each and every object in that bucket to check both are same objects. if it finds the one, contains(Object) method will return true.
 
Gyanesh Sharma
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys. I guess I got it now. Correct me if I am wrong.

The only difference in the HashSet and HashMap is the difference in Set and Map. In other words, HashSet and HasMap are implemented using the hashCode() and equals() method only. In HashSet you use only one object to search or insert, whereas you use the pair of two objects key Object and value Object in case of the HashMap. In HashMap, hashCode is applied on a unique key (Object) therefore hashCode value of the key should be always unique. Whereas on HashSet hashCode() value of the member objects could be duplicaated as long as it is not equal (equals() return false) to any of the existing objects in the HashSet.
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks like HashMap maintains those so called buckets for both keys and values . One is used when put-ing and the other is used with values when contains() is called . correct me if I am wrong .
 
Ramakrishna RangaRaoOne AllamOne
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess even in case of HashMap also there can be two keys with the same hashcode(), equals() of which being true . same as HashSet.
 
Gyanesh Sharma
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not sure if you are wrong because I am just a newbie in Java. But I tend to think that the hashCode buckets are maintained only for the key objects and those bucket not only represent the hashCode but it also has a reference (address) to the corresponding value object.
I would highly appreciate the java gurus out there in the Ranch to help us out.
 
Gyanesh Sharma
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you made me think....
So if hashCode() of the key Objects can return duplicate results then hashMap is equivalent to two hashSet, one for the key Object and another for the value Object. Therefore enabling users to search on the key or the value.
I'm not sure if that is true. I hope I will find the answer soon.
 
    Bookmark Topic Watch Topic
  • New Topic