Search This Blog

Difference between Hashmap & Hashtable

hashmap vs hashtable

hashmap and hashtable both implement java.util.map interface which means both are implementing same methods but with few differences.Understanding those difference will help us in writing more efficient code.
  1. First and foremost difference between both is that hashtable is synchronized whereas hashmap is not which simply suggest that hashtable is a thread safe.In a multi-threaded env threads have to acquire lock on object before accessing them and rest of the threads have to wait until the first thread is done processing hashtable.To overcome this, Java 5 came up with Concurrent hashmap which is synchronized and it provides better scalability than hashtable.
  2. hashtable doesn't allow "null" whereas hashmap allows one "null" key and multiple "null" values.
  3. In order to comply with thread safety & synchronization, hashtable are much slower than hashmaps, so if its a non-threaded environment use of hashmap would be a good choice or if you are running Java 5 consider Concurrent hashmap.
  4. There is no ordering of key-value pair in hashmap.

No comments:

Post a Comment