What is the difference between a HashMap and a TreeMap?

Java

Java Problem Overview


I started learning Java. When would I use a HashMap over a TreeMap?

Java Solutions


Solution 1 - Java

TreeMap is an example of a SortedMap, which means that the order of the keys can be sorted, and when iterating over the keys, you can expect that they will be in order.

HashMap on the other hand, makes no such guarantee. Therefore, when iterating over the keys of a HashMap, you can't be sure what order they will be in.

HashMap will be more efficient in general, so use it whenever you don't care about the order of the keys.

Solution 2 - Java

HashMap is implemented by Hash Table while TreeMap is implemented by Red-Black tree. The main difference between HashMap and TreeMap actually reflect the main difference between a Hash and a Binary Tree , that is, when iterating, TreeMap guarantee can the key order which is determined by either element's compareTo() method or a comparator set in the TreeMap's constructor.

Take a look at following diagram.

enter image description here

Solution 3 - Java

To sum up:

  • HashMap: Lookup-array structure, based on hashCode(), equals() implementations, O(1) runtime complexity for inserting and searching, unsorted
  • TreeMap: Tree structure, based on compareTo() implementation, O(log(N)) runtime complexity for inserting and searching, sorted

Taken from: [HashMap vs. TreeMap][1]

[1]: http://arnosoftwaredev.blogspot.com/2010/10/hash-tables-vs-binary-search-tree.html "HashMap vs. TreeMap"

Solution 4 - Java

Use HashMap most of the times but use TreeMap when you need the key to be sorted (when you need to iterate the keys).

Solution 5 - Java

I'll talk about the HashMap and TreeMap implementation in Java:

  • HashMap -- implement basic map interface
    1. implemented by an array of buckets, each bucket is a LinkedList of entries
    2. running time of basic operations: put(), average O(1), worst case O(n), happens when the table is resized; get(), remove(), average O(1)
    3. not synchronized, to synchronize it: Map m = Collections.synchronizedMap(new HashMap(...));
    4. Iteration order of the map is unpredictable.
  • TreeMap -- implement navigable map interface
    1. implemented by a red-black tree
    2. running time of basic operations: put(), get(), remove(), worst case O(lgn)
    3. not synchronized, to synchronize it: SortedMap m = Collections.synchronizedSortedMap(new TreeMap(...));
    4. provide ordered iteration. higherKey(), lowerKey() can be used to get the successor and predecessor of a given key.

To sum, the biggest difference between HashMap and TreeMap is that TreeMap implements NavigableMap<K,V>, which provide the feature of ordered iteration. Besides, both HashMap and TreeMap are members of Java Collection framework. You can investigate the source code of Java to know more about their implementations.

Solution 6 - Java

You almost always use HashMap, you should only use TreeMap if you need your keys to be in a specific order.

Solution 7 - Java

HashMap is used for fast lookup, whereas TreeMap is used for sorted iterations over the map.

Solution 8 - Java

Along with sorted key store one another difference is with TreeMap, developer can give (String.CASE_INSENSITIVE_ORDER) with String keys, so then the comparator ignores case of key while performing comparison of keys on map access. This is not possible to give such option with HashMap - it is always case sensitive comparisons in HashMap.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionkkdas02View Question on Stackoverflow
Solution 1 - JavaTM.View Answer on Stackoverflow
Solution 2 - JavapierrotlefouView Answer on Stackoverflow
Solution 3 - JavaSiriusView Answer on Stackoverflow
Solution 4 - JavanandaView Answer on Stackoverflow
Solution 5 - Javauser2060386View Answer on Stackoverflow
Solution 6 - JavaJornView Answer on Stackoverflow
Solution 7 - JavafastcodejavaView Answer on Stackoverflow
Solution 8 - JavaDhwanitView Answer on Stackoverflow