GeekInterview.com
Series: Subject: Topic:
Question: 177 of 576

What is the difference between hashmap and hashset?

Asked by: naveen.anamaneni | Member Since May-2007 | Asked on: May 4th, 2007

View all questions by naveen.anamaneni

Showing Answers 1 - 12 of 12 Answers
ankit

Answered On : Jul 23rd, 2007

HashMap implements Map interface.
hashset implements Set interface. hashset is unordered.

  
Login to rate this answer.
kARUNA

Answered On : Jul 23rd, 2007

HashMap is not collection inteface where as HashSet is Collection interface.
HashMap class implements Map interface where as HashSet class implements Set interface.
HashMap is prints the elements orderly where as HashSet has no order, if you need some order then go for TreeSet.

  
Login to rate this answer.

HashMapimplementsMapinterface. It stores unique keys.
HashSetimplements Setinterface. It stores uniqueelements.

  
Login to rate this answer.
sampra

Answered On : Feb 14th, 2008

View all answers by sampra

given ans are    not good but if you make it integrated then it will be ok

  
Login to rate this answer.
crmwind

Answered On : Nov 20th, 2008

View all answers by crmwind

A HashMap provides fast access to a mapping from a unique key to a value.  Keys are  unordered, which makes it faster than the TreeMap, where the keys are ordered.  A HashSet is fast access to unique values only (there are no keys because it is not a mapping, it's just the values).  HashSet values are unordered, which makes it faster than the TreeSet, where the values are ordered.

Yes  3 Users have rated as useful.
  
Login to rate this answer.



hash map stores the date/values/objects in key-value pairs and where as hashset stores the objects serially.

  
Login to rate this answer.
spasha1

Answered On : Dec 3rd, 2008

View all answers by spasha1

HashSet::there is no name and value pair.
HashMap:There is name and value pair.

HashSet:It belongs to Set interface.
HashMap:It belongs to Map interface.

HashSet:it belongs to abstratctset class.
HashMap:it belongs to abstractmap class.

 

  
Login to rate this answer.
pawan.kumar

Answered On : Apr 28th, 2009

View all answers by pawan.kumar

HashMap: contains unique Key/value pair, from Map hiererchy.
Map map = new HashMap<K,V>();

HashSet: contains unique Element, from Collenction class hiererchy.
Set set = new HashSet();

  
Login to rate this answer.
chidananda

Answered On : Aug 14th, 2011

HASHSET: -->hash set is the one of the set implementation -->it does not allow duplicates -->it allows nulls -->it is not thread safe,but it can be made as thread safe by using Collection utility HASH MAP: -->it is one of the map implementation whose underlying data structure is HASHSET. -->it is much faster than the hash set ,bcoz the values are associated to unique key -->it does not allows duplicates . -->it is not thread safe.

Code
  1. package com.chiddu.util.map;
  2. import java.System.*;
  3. public class HashMapSample
  4. {
  5. public static  void  main(String...s)
  6. {
  7. //inserting elements
  8. HashMap map=new HashMap();
  9. //store key and value pairs
  10. map.put("a1","b1");
  11. map.put("a2","b2");
  12.  
  13. map.put("a3","b3");
  14.  
  15. map.put("a4","b4");
  16.  
  17. map.put("a5","b5");
  18. map.put("null","null");
  19. //retriving the elements for a given key
  20. System.out.println(map.get("a1"));
  21. //retriving all the elements from the HAshMap
  22. Set set=map.KeySet();
  23. Iterator it=Set.iterator();
  24. while(it.hasNext())
  25. {
  26. Object key=it.next();
  27. System.out.println(key+"="+map.get(key));
  28. }
  29.  
  30. }
  31. }

  
Login to rate this answer.
ravi

Answered On : Sep 12th, 2011

I was reading about collection framework of Java. And was studying Hashtable, HashMap and HashSet. Its quite interesting to know the differences between them. In this post I will discuss these three with examples.

Hashtable:

Hashtable is basically a datastructure to retain values of key-value pair.

It didnt allow null for both key and value. You will get NullPointerException if you add null value.
It is synchronized. So it comes with its cost. Only one thread can access in one time

//example1
Hashtable; cityTable = new Hashtable();
cityTable.put(1, "Lahore");
cityTable.put(2, "Karachi");
cityTable.put(3, null); /* NullPointerEcxeption at runtime*/

System.out.println(cityTable.get(1));
System.out.println(cityTable.get(2));
System.out.println(cityTable.get(3));

HashMap:

Like Hashtable it also accepts key value pair.

It allows null for both key and value
It is unsynchronized. So come up with better performance


//example2
HashMap productMap = new HashMap();
productMap.put(1, "Keys");
productMap.put(2, null);

HashSet:

HashSet does not allow duplicate values. It provides add method rather put method. You also use its contain method to check whether the object is already available in HashSet. HashSet can be used where you want to maintain a unique list.
//example3
HashSet stateSet = new HashSet();
stateSet.add ("CA");
stateSet.add ("WI");
stateSet.add ("NY");

if (stateSet.contains("PB")) /* if CA, it will not add but shows following message*/
System.out.println("Already found");
else
stateSet.add("PB");

  
Login to rate this answer.
maheshjammula

Answered On : Nov 28th, 2011

hashSet and hashMap both are not maintained order.Difference is hashMap contains key value to map the corresponding value but hashSet have no key value and index value.Their implementations is another matter.

  
Login to rate this answer.
sampra

Answered On : Mar 6th, 2012

View all answers by sampra

HashMap is not collection inteface where as HashSet is Collection interface.
HashMap class implements Map interface where as HashSet class implements Set interface.
HashMap is prints the elements orderly where as HashSet has no order, if you need some order then go for TreeSet.

  
Login to rate this answer.

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

Related Open Questions

Ads

Connect

twitter fb Linkedin GPlus RSS

Ads

Interview Question

 Ask Interview Question?

 

Latest Questions

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.