To the point difference between the different map collection type

The following are the important differences between TreeMap, HashMap and LinkedHashMap.

Sr. No.KeyTreeMapHashMapLinkedHashMap
1Ordering of elementsThe elements inserted in TreeMap are sorted according to the natural ordering of its keys, or by a Comparator provided at map creation time, depending on which constructor is used.In case of HashMap it does not guarantees as to the order of the map also it does not guarantee that the order will remain constant over time.LinkedHashMap follows the insertion order of elements and also maintaining an order of elements inserted into it.
2Internal implementationThe internal implementation of TreeMap does not allow storing null key but only null values are allowed.In the case of HashMap storing one null key as well as of multiple null values are allowed.LinkedHashmap is internally implemented more similar as HashMap so storing of one null key and of multiple null values are allowed.
3Operational ComplexityTreeMap comes with the complexity of its get,put and remove operations as O(log(n)), which is greater than that of HashMapHashMap on other hand has the complexity of O(1) in case of its get,put and remove operations.LinkedHashMap again has the same complexity as of HashMap i.e O(1).
4InheritanceTreeMap implements SortedMap interface of Collection framework which is a child of Map.And internally TreeMap implements Red-Black Tree(a Self Balancing Binary Search Tree).On other hand HashMap implements simple Map interface and internally uses hashing for storing and retrieval of its elements.Alike of TreeMap LinkedHashMap extends HashMap and internally uses hashing as like in HashMap.
5Index performanceTreeMap is maintaining order of its elements hence is lesser in performance index and also requires more memory than HashMap and LinkedHashMap.HashMap as do not maintain any insertion order of its elements hence is faster as compare to TreeMap also do not sort its elements on the basis of its value so also faster than LinkedHashMap.LinkedHashMap is faster as compare to TreeMap but is slower than HashMap.
6ComparisonElements in TreeMap get compared by using compareTo() method in which custom implementation could also be provided.On other hand HashMap uses compare() method of Object class for its elements comparison.LinkedHashMap also uses compare() method of Object class for its elements comparison.
50% LikesVS
50% Dislikes

Leave a Reply

Your email address will not be published. Required fields are marked *