This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Difference in HashMap and HashSet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Difference in HashMap and HashSet" Watch "Difference in HashMap and HashSet" New topic
Author

Difference in HashMap and HashSet

Gyanesh Sharma
Ranch Hand

Joined: Nov 27, 2005
Posts: 47
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.
james edwin
Ranch Hand

Joined: Nov 22, 2001
Posts: 393
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


Regards,
James
Gyanesh Sharma
Ranch Hand

Joined: Nov 27, 2005
Posts: 47
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?
Prabhu Venkatachalam
Ranch Hand

Joined: Nov 16, 2005
Posts: 502

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.


Prabhu Venkatachalam<br />SCJP 1.4,SCWCD 1.4<br />prabhu.venkatachalam@gmail.com
Gyanesh Sharma
Ranch Hand

Joined: Nov 27, 2005
Posts: 47
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.
Ramakrishna RangaRaoOne AllamOne
Greenhorn

Joined: Nov 28, 2005
Posts: 18
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

Joined: Nov 28, 2005
Posts: 18
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

Joined: Nov 27, 2005
Posts: 47
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

Joined: Nov 27, 2005
Posts: 47
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Difference in HashMap and HashSet
 
Similar Threads
Default load factor 0.75. why ?
Adding duplicate values into HashSet
Hashset, Hashtable & Hashmap
Hashing
Adding Elements