HashMap vs Hashtable

HashMap vs Hashtable

Hashtable and HashMap are both key-value based data structure. Both of them allows the access data based on key.
Both of them some differences in storing values and performance over iteration.

Some of the basic differences are following

HashMap HashTable
Synchronized Un-synchronized  Synchronized
Allow null Allowed Null for key and Value Not allowed, Null pointer would be thrown for Null
 Support in JDK since 1.2  1.0
 Subclass of Dictionary AbstractMap
 load factor  .75 .75

Hashtable

Hashtable performance is effected by its initial capacity and load factor provided at creation of object. In case of high load factor it grows it self.
At the time of creation there should not be too much initial capacity other-wise it would wastage of space. Higher the value of load factor will save to space, but lower the load factor will increase the time for searching entry.

Hashmap

Hashmap provide no guarantees to the order of the map. Hashmap is not synchronized, It means that multiple thread can access the same instance at same time, which can lead to
structal modification.

HashMap is better for non-threaded applications, as unsynchronized Objects perform better than synchronized ones.

Java bug

I am Java professional, working on Enterprise Java from last 8 years.

More Posts - Website

Follow Me:
TwitterLinkedIn