What is the difference between hashmap and hashset?

Questions by naveen.anamaneni

Showing Answers 1 - 36 of 36 Answers

ankit

  • Jul 23rd, 2007
 

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

  Was this answer useful?  Yes

kARUNA

  • 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.

  Was this answer useful?  Yes

crmwind

  • Nov 20th, 2008
 

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.

spasha1

  • Dec 3rd, 2008
 

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.

 

  Was this answer useful?  Yes

pawan.kumar

  • Apr 28th, 2009
 

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<E>();

  Was this answer useful?  Yes

chidananda

  • 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. }

  Was this answer useful?  Yes

ravi

  • 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 didn’t 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");

  Was this answer useful?  Yes

maheshjammula

  • 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.

  Was this answer useful?  Yes

sampra

  • Mar 6th, 2012
 

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.

  Was this answer useful?  Yes

Give your answer:

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

 

Related Answered Questions

 

Related Open Questions