Friday, October 7, 2016

Hash Map and Hash Table Difference

HashMap and Hashtable both are used to store data in key and value form. Both are using hashing technique to store unique keys.

But there are many differences between HashMap and Hashtable classes that are given below.

HashMap vs Hashtable

1) HashMap is non synchronized. It is not-thread safe and can't be shared between many threads without proper synchronization code.
Hashtable is synchronized. It is thread-safe and can be shared with many threads.

2) HashMap allows one null key and multiple null values.
Hashtable doesn't allow any null key or value.

3) HashMap is a new class introduced in JDK 1.2.
Hashtable is a legacy class.

4) HashMap is fast.
Hashtable is slow.

5) We can make the HashMap as synchronized by calling this code
Map m = Collections.synchronizedMap(hashMap);
Hashtable is internally synchronized and can't be unsynchronized.

6) HashMap is traversed by Iterator.
Hashtable is traversed by Enumerator and Iterator.

7) Iterator in HashMap is fail-fast.
Enumerator in Hashtable is not fail-fast.
8) HashMap inherits AbstractMap class.
Hashtable inherits Dictionary class.


Example of Fail Fast Iterator and Fail Safe Iterator


import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class FailFastExample
{
    
    
    public static void main(String[] args)
    {
        Map<String,String> premiumPhone = new HashMap<String,String>();
        premiumPhone.put("Apple", "iPhone");
        premiumPhone.put("HTC", "HTC one");
        premiumPhone.put("Samsung","S5");
        
        Iterator iterator = premiumPhone.keySet().iterator();
        
        while (iterator.hasNext())
        {
            System.out.println(premiumPhone.get(iterator.next()));
            premiumPhone.put("Sony", "Xperia Z");
        }
        
    }
    
}

Output :


iPhone 
Exception in thread "main" java.util.ConcurrentModificationException
        at java.util.HashMap$HashIterator.nextEntry(Unknown Source)
        at java.util.HashMap$KeyIterator.next(Unknown Source)
        at FailFastExample.main(FailFastExample.java:20)


Fail Safe Iterator Example :


import java.util.concurrent.ConcurrentHashMap;
import java.util.Iterator;


public class FailSafeExample
{
    
    
    public static void main(String[] args)
    {
        ConcurrentHashMap<String,String> premiumPhone = 
                               new ConcurrentHashMap,String>();
        premiumPhone.put("Apple", "iPhone");
        premiumPhone.put("HTC", "HTC one");
        premiumPhone.put("Samsung","S5");
        
        Iterator iterator = premiumPhone.keySet().iterator();
        
        while (iterator.hasNext())
        {
            System.out.println(premiumPhone.get(iterator.next()));
            premiumPhone.put("Sony", "Xperia Z");
        }
        
    }
    
}

Output :
S5
HTC one
iPhone







Fail Fast IteratorFail Safe Iterator
Throw ConcurrentModification ExceptionYesNo
Clone objectNoYes
Memory OverheadNoYes
ExamplesHashMap,Vector,ArrayList,HashSet
CopyOnWriteArrayList,
ConcurrentHashMap


No comments:

Post a Comment

உப்பு மாங்காய்

சுருக்குப்பை கிழவி. சுருக்கங்கள் சூழ் கிழவி. பார்க்கும் போதெல்லாம் கூடையுடனே குடியிருப்பாள். கூடை நிறைய குட்டி குட்டி மாங்காய்கள். வெட்டிக்க...