Tuesday, 20 September 2016

HashMap vs Hashtable vs HashSet


  Here I am going to explain difference between HashMap vs HasTable vs HashSet 

HashMap 

     HashMap accepts key-value pair,It allows null value for both key and Value and HashMap is Unsynchronized.

  HashMap<String,String> hashmap=new HashMap<String,String>();

  hashmap.put("1","Dhina");
  hashmap.put("2",null);

HasTable 

 HasTable accepts key-value pair,It didn't allows null value for both key and Value and HasTable is Synchronized.


  HasTable <String,String> hashtable=new HasTable <String,String>();

  hashtable.put("1","Dhina");
  hashtable.put("2","Karan");


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.


  HashSet<String> set= new HashSet<String>();
  set.add ("DH");
  set.add ("IN");