Wednesday, July 10, 2013

HashMap Vs HashTable

                             HashMap is HashTable based implementation of Map Interface. It doesn't follow any order. This class makes no guarantees as to the order of the Map and it doesn't guarantee that the order will remain constant over time. HashMap allows null valuers and null key. An instance of HashMap has  two parameters that affect its performance: initial capacity and load factor. The default load factor of HashMap is 0.75.
                            HashTable is implemented based on Key-Value pair data-Structure in java. It extends Dictionary and implements Map. In this Key should be unique and allow value as duplicate. It also implement hashCode() and equals() methods for comparing duplicate key.

Difference between HashMap and HashTable:

1. HashMap using  HashTable for storing key-value pairs.but HashTable uses key-value pair data structure.

2. Iterator in HashMap is Fail-Fast but enumaretion in HashTable in not Fail-Fast.

3. HashMap is not Synchronized and HashTable is Synchronized.But we can get Synchronized HashMap by using Collections class method synchronizedMap() .

4.  HashMap is HashTable based implementation of Map Interface. HashTable extends Dictionary and implements Map.

5. HashMap allows one null key and any number of null values but HashTable doesn't allows null keys and null values.

6. HashTable is not Threadsafe and HashTable is  Threadsafe.

7. HashMap is faster than HashTable.

8. We can use ConcurrentHashMap for multi-thread environment.But HashTable doesn't contain this type of facility.
Can we create Synchronization for HashMap?

Yes,We can achieve this by using Collections.synchronizedMap();

HashMap hs=new  HashMap();
Map m=Collections.synchronizedMap(hs);



No comments:

Post a Comment