June 29, 2022

HashMap Vs LinkedHashMap Vs TreeMap Vs HashTable in Java

If you have to store (key, value) pair in your Java application you will use one of the hash table based implementation present in java.util package and the options are HashMap, LinkedHashMap, TreeMap and HashTable. The Map implementation chosen by you will be based on the criteria whether synchronization is required, any ordering is required or not and the performance. Since the functionality offered by the Map implementations differ so there must be some differences among the HashMap, LinkedHashMap and TreeMap and that is what we’ll see in this post.

HashMap Vs LinkedHashMap Vs TreeMap Vs HashTable in Java

1- First criteria is synchronization.

  • HashMap- HashMap is not synchronized, if it has to be used in a multi-threaded environment then HashMap has to be synchronized externally using Collections.synchronizedMap() method.
  • LinkedHashMap- LinkedHashMap is not synchronized, if it has to be used in a multi-threaded environment then it has to be synchronized externally using Collections.synchronizedMap() method.
  • TreeMap- TreeMap is not synchronized, if it has to be used in a multi-threaded environment then it has to be synchronized externally using Collections.synchronizedSortedMap() method.
  • HashTable- Hashtable is synchronized so provides thread safety.

2- Second criteria is ordering.

  • HashMap- HashMap is an unordered Map implementation.
  • LinkedHashMap- Maintains either the insertion order or the access order (from least-recently accessed to most-recently) based on the constructor used to construct a LinkedHashMap.
  • TreeMap- In TreeMap elements are sorted based on their natural ordering by default. If you want any custom sorting then you will have to provide a Comparator.
  • HashTable- HashTable is also unordered.

3- Third criteria is allowing null as key and value.

  • HashMap- Allows null as key as well as for values. Only single null key is permitted though more than one null values are permitted.
  • LinkedHashMap- Allows null as key as well as for values. Only single null key is permitted though more than one null values are permitted.
  • TreeMap- TreeMap doesn’t permit null as key. NullPointerException is thrown if you try to add null as key. Null value is permitted.
  • HashTable- Same as HashMap.

4- Fourth criteria is internal implementation.

  • HashMap- In HashMap internal implementation an array of type Node is used to store the elements. Each index of the array is considered one bucket.
  • LinkedHashMap- Extends HashMap and uses the same internal storage as HashMap. LinkedHashMap also maintains a doubly-linked list running through all of its entries. This linked list defines the iteration ordering.
  • TreeMap- TreeMap uses a tree structure where element of type Entry are stored. TreeMap is a Red-Black tree based NavigableMap implementation.
  • HashTable- In HashTable internally an array of type Entry is used to store the elements.

5- Fifth criteria is default initial capacity.

  • HashMap- Default initial capacity is 16.
  • LinkedHashMap- Default initial capacity is 16.
  • TreeMap- No default capacity as array is not used.
  • HashTable- Default initial capacity is 11.

6- Sixth criteria is performance.

  • HashMap-HashMap provides constant-time performance for the basic operations (get and put), With the assumption that the hash function is distributing the elements properly among the buckets.
  • LinkedHashMap- Performance of LinkedHashMap is likely to be just slightly below that of HashMap, due to the added expense of maintaining the doubly linked list.
  • TreeMap- TreeMap provides guaranteed log(n) time cost for the containsKey, get, put and remove operations.
  • HashTable- All the methods of HashTable are synchronized on a single lock so it is slow.

That's all for the topic HashMap Vs LinkedHashMap Vs TreeMap Vs HashTable in Java. If something is missing or you have something to share about the topic please write a comment.


You may also like

No comments:

Post a Comment