HashMap and TreeMap are the Map classes and both implements the Map interface. Map is an object that stores key-value pairs, where each key is unique and but there may be duplicate values. The HashMap class uses the hash table as a data structure. The TreeMap uses the red-black tree as a data structure.
The main difference between HashMap and Treemap is that the HashMap does not preserve the insertion order whereas, the Treemap does. So let us begin our discussion on the differences between HashMap and TreeMap with the help of the comparison chart shown below.
Content: HashMap Vs TreeMap
|Basis for Comparison||HashMap||TreeMap|
|Basic||HashMap does not maintain insertion order.||TreeMap maintains insertion order.|
|DataStructure||HashMap uses Hash Table as an underlying data structure.||TreeMap uses Red-Black Tree as an underlying data structure.|
|Null Keys and Values||HashMap allows Null key once ad Null value any number of time.||TreeMap does not allow Null key but allows Null Values any number of time.
|Extends and Implements||HashMap extends AbstractMap class and implements Map interface.||TreeMap extends AbstractMap class and implements SortedMap and NavigableMap interface.|
|Performance||HashMap operates faster.||TreeMap in comparison to HashMap operates slower.|
Definition of HashMap
HashMap is a Map class. It uses the hash table, as a data structure to store the maps key value pair. The insertion of key-value pair is done using the hash code of the keys. Hence, each key in the map must be unique as it will be used to retrieve the values.
The insertion order in HashMap is not preserved which means that the hashmap object does not return the elements in the order they were inserted. On the other hands, the order in which the elements will be returned is not fixed.
The key is allowed to be NULL at once, but the values can be NULL at any number of time. The HashMap can contain the heterogeneous objects for keys as well as values.
There are four constructors of HashMap:
HashMap( ) HashMap(Map m) HashMap(int capacity), HashMap(int capacity, float fillRatio)
The first constructor creates the empty object of HashMap. The second constructor initializes the HashMap using elements of Map m. The third constructor initializes the HashMap with the capacity provided in the argument. The fourth constructor initializes the capacity as well as the fill ratio of the HashMap object.
The default capacity of the HashMap is 16, and the default fill ratio of the HashMap is 0.75.
Definition of TreeMap
Like HashMap, TreeMap is also a Map class. TreeMap extends AbstractMap class and implements NavigabelMap and SortedMap. The TreeMap objects stores the map elements in the tree structure. The data structure used for storing the Map is the Red-Black tree.
The TreeMap stores the key value pair in the sorted order which helps in fast retrieval of the elements. The TreeMap object returns the elements in the sorted (ascending) order.
There are four constructors of TreeMap:
TreeMap( ) TreeMap(Comparator<? super K> comp) TreeMap(Map<? extends K, ? extends V> m) TreeMap(SortedMap<K, ? extends V> sm)
The first constructors create an empty object of TreeMap that would be sorted in natural order its keys. The second constructor will create an empty tree map that will be sorted by the Comparator cmp. The third constructor above will create a treemap that will be initialized using entries of Map m. The fourth constructor will create a treemap that will be initialized using the entries of SortedMap sm.
Treemap does not have any new method of its own it uses the method of interface NavigableMap and SortedMap and the AbstractMap class.
Key Differences Between HashMap and TreeMap
- Both the classes are used to create map objects, but the basic difference between HashMap and Treemap is that HashMap does not maintain insertion order whereas the Treemap does.
- The data structure used by Hashmap to store map elements is the hash table and the data structure used by TreeMap to store the map elements is the red-black tree.
- Both the classes Hashmap and Treemap extends AbstractMap class, but the HashMap class implements Map interface and the TreeMap implements NavigableMap and SortedMap interface.
- The values can be Null any number of time in both but the key is allowed to be Null only once in HashMap and a key can never be in Treemap.
- The performance of HashMap is faster it does not waste time on sorting the map elements as TreeMap does. Hence, TreeMap performs slower than HashMap.
TreeMap should be used only when you require key value pair in sorted form. As sorting includes performance cost. HashMap being unsynchronized operates faster.