Piano Guidance
Photo by Kassandre Pedro Pexels Logo Photo: Kassandre Pedro

What is the difference between HashMap and TreeMap?

HashMap allows a single null key and multiple null values. TreeMap does not allow null keys but can have multiple null values. HashMap allows heterogeneous elements because it does not perform sorting on keys. TreeMap allows homogeneous values as a key because of sorting.

Who kicks more boy or girl?
Who kicks more boy or girl?

One study, published in 2001 in the journal Human Fetal and Neonatal Movement Patterns, found that boys may move around more in the womb than...

Read More »
What is a scherzo and trio?
What is a scherzo and trio?

The scherzo itself is a rounded binary form, but, like the minuet, is usually played with the accompanying trio followed by a repeat of the...

Read More »

next → ← prev Difference between HashMap and TreeMap Java HashMap and TreeMap both are the classes of the Java Collections framework. Java Map implementation usually acts as a bucketed hash table. When buckets get too large, they get transformed into nodes of TreeNodes, each structured similarly to those in java.util.TreeMap. HashMap HashMap implements Map, Cloneable and Serializable interface. It extends AbstractMap class. It belongs to java.util package. HashMap contains value based on the key.

It may have a single null key and multiple null values.

HashMap does not maintain order while iterating.

It contains unique elements.

It works on the principle of hashing. TreeMap TreeMap class extends AbstractMap class and implements NavigableMap, Cloneable, and Serializable interface. TreeMap is an example of a SortedMap. It is implemented by the Red-Black tree, which means that the order of the keys is sorted. TreeMap also contains value based on the key.

TreeMap is sorted by keys.

It contains unique elements.

It cannot have a null key but have multiple null values.

Keys are in ascending order.

It stores the object in the tree structure. Similarities between HashMap and TreeMap HashMap and TreeMap classes implement Cloneable and Serializable interface.

and classes implement and interface. Both the classes extend AbstractMap class.

class. A Map is an object which stores key-value pairs. In the key-value pair, each key is unique, but their values may be duplicate . pairs. In the key-value pair, each key is unique, but their values may be . Both classes represents the mapping from key to values .

to . Both maps are not synchronized .

. Map use put() method to add an element in the map.

method to add an element in the map. The iterator throws a ConcurrentModificationException if the map gets modify in any way. The Key difference between HashMap and TreeMap is: HashMap does not preserve the iteration order while the TreeMap preserve the order by using the compareTo() method or a comparator set in the TreeMap's constructor. The following table describes the differences between HashMap and TreeMap. Basis HashMap TreeMap Definition Java HashMap is a hashtable based implementation of Map interface. Java TreeMap is a Tree structure-based implementation of Map interface. Interface Implements HashMap implements Map, Cloneable, and Serializable interface. TreeMap implements NavigableMap, Cloneable, and Serializable interface. Null Keys/ Values HashMap allows a single null key and multiple null values. TreeMap does not allow null keys but can have multiple null values. Homogeneous/ Heterogeneous HashMap allows heterogeneous elements because it does not perform sorting on keys. TreeMap allows homogeneous values as a key because of sorting. Performance HashMap is faster than TreeMap because it provides constant-time performance that is O(1) for the basic operations like get() and put(). TreeMap is slow in comparison to HashMap because it provides the performance of O(log(n)) for most operations like add(), remove() and contains(). Data Structure The HashMap class uses the hash table. TreeMap internally uses a Red-Black tree, which is a self-balancing Binary Search Tree. Comparison Method It uses equals() method of the Object class to compare keys. The equals() method of Map class overrides it. It uses the compareTo() method to compare keys. Functionality HashMap class contains only basic functions like get(), put(), KeySet(), etc. . TreeMap class is rich in functionality, because it contains functions like: tailMap(), firstKey(), lastKey(), pollFirstEntry(), pollLastEntry(). Order of elements HashMap does not maintain any order. The elements are sorted in natural order (ascending). Uses The HashMap should be used when we do not require key-value pair in sorted order. The TreeMap should be used when we require key-value pair in sorted (ascending) order. Example of HashMap vs TreeMap In the following example, we can observe that the elements of the HashMap is in random order while the elements of the TreeMap is arranged in ascending order. import java.util.Map; import java.util.HashMap; import java.util.TreeMap; public class HashMapVsTreeMapExample { public static void main(String args[]) { /*------------HashMap implementation----------------*/ Map hm=new HashMap(); //adding elements to the HashMap hm.put(9, "Red"); hm.put(12, "Black"); hm.put(6, "Green"); hm.put(19, "White"); System.out.println("HashMap iteration Order:"); //iteration over map using for each loop for(Map.Entryentry: hm.entrySet()) { //getting keys and values using method System.out.println(entry.getKey() + " = " +entry.getValue()); } /*------------TreeMap implementation----------------*/ Map tm=new TreeMap(); //adding elements to the TreeMap tm.put(9, "Red"); tm.put(12, "Black"); tm.put(6, "Green"); tm.put(19, "White"); System.out.println("TreeMap iteration Order:"); //iteration over map using for each loop for(Map.Entryentry: tm.entrySet()) { //getting keys and values using method //prints list in sorted order System.out.println(entry.getKey() + " = " +entry.getValue()); } } } Output: Next Topic Java Tutorial

← prev next →

Is C7 the same as C dominant 7?
Is C7 the same as C dominant 7?

The C7 - or C dominant seventh chord -- has a quavery brightness to its tone. It can create a hesitant, tentative mood in a song, or can play with...

Read More »
Does Chopin have perfect pitch?
Does Chopin have perfect pitch?

Some of the greatest classical composers, including Mozart, Beethoven, Chopin and Handel all had perfect pitch; and it's not so rare in the pop...

Read More »

What are the 2 types of sonatas?
What are the 2 types of sonatas?

As with the cantata, in the mid-Baroque there was a tendency to divide trio sonatas into two categories: sontata da camera and sonata da chiesa....

Read More »
Is caramel pronounced Carmel or caramel?
Is caramel pronounced Carmel or caramel?

Carmel vs Caramel Pronunciation There's no single correct way to pronounce caramel. The three most popular pronunciations of caramel are: “KARR-...

Read More »
How long does it take to learn the piano?
How long does it take to learn the piano?

If you want to be a professional classical performer, you're looking at a minimum of 10 to 15 years of concentrated study with a master teacher,...

Read More »
What key is most hits in?
What key is most hits in?

C major and G major, along with their relative minor counterparts A minor and E minor, are often considered the best key and scales for Pop music....

Read More »