To the point difference between the different map collection type
The following are the important differences between TreeMap, HashMap and LinkedHashMap.
|1||Ordering of elements||The 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.|
|2||Internal implementation||The 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.|
|3||Operational Complexity||TreeMap comes with the complexity of its get,put and remove operations as O(log(n)), which is greater than that of HashMap||HashMap 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).|
|4||Inheritance||TreeMap 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.|
|5||Index performance||TreeMap 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.|
|6||Comparison||Elements 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.|