Sort Map by Values in Java

In this post, we will discuss various methods to sort Map by values in Java i.e. sort map according to the natural ordering of its values.

 

1. Using TreeMap

TreeMap is a Red-Black tree based implementation of Map which is sorted according to comparator passed to its constructor. By writing custom comparator to the TreeMap, we can sort the map according to the natural ordering of its values as shown below.

Java

Download   Run Code

Output:

Sorted Map by Values :
{China=Beijing, India=New Delhi, Japan=Tokyo, USA=Washington D.C.}

 

Using Guava’s TreeMap –

Google’s Guava library also provides TreeMap implementation which we can use to create a mutable, empty TreeMap instance which is sorted according to a Comparator passed to its constructor.

Download   Run Code

 

2. Using LinkedHashMap

LinkedHashMap is a hash table and linked list implementation of the Map interface, with predictable iteration order which is the order in which values were inserted into the map. We can use this property to produce a copy of a map that is sorted according to the natural ordering of its values.

  1. Create a list of map entries and sort it based on its values.
     
  2. Create an empty LinkedHashMap and for every map entry in the sorted list, insert key-value pair in it.

The resultant LinkedHashMap will be sorted by values.

Java

Download   Run Code

Output:

Sorted Map by Values :
{China=Beijing, India=New Delhi, Japan=Tokyo, USA=Washington D.C.}

 

3. Java 8

We can also use Java 8 Stream API to sort Map by values. Below are the steps:

  1. Obtain stream from Set view of the mappings contained in the map.
     
  2. Sort the stream in natural order of values using Stream.sorted() method by passing comparator returned by Map.Entry.comparingByValue().
     
  3. Collect all sorted elements in a LinkedHashMap using Stream.collect() with Collectors.toMap().

 
Please note that a Stream is a sequence of items, not a sequence of key/value pairs. So we can’t construct a Map out of a stream without specifying how to extract values and values from it. Java 8 provides Collectors.toMap() method for this purpose. We need to use overloaded version of toMap() that returns LinkedHashMap instead of HashMap to retain the sorted order.

Java

Download   Run Code

Output:

Sorted Map by Values :
{China=Beijing, India=New Delhi, Japan=Tokyo, USA=Washington D.C.}

Below is another approach that don’t use Java 8 collectors. It uses Stream.forEachOrdered() method on the sorted stream that inserts each element of the stream into a new LinkedHashMap.

Java

Download   Run Code

Output:

Sorted Map by Values :
{China=Beijing, India=New Delhi, Japan=Tokyo, USA=Washington D.C.}

 
Related Post: Sort Map by keys in Java

 
Exercise: Modify above code to sort Map by reverse ordering of its values

 
References:

1. TreeMap Javadoc – Java SE 8
2. LinkedHashMap Javadoc – Java SE 8 Javadoc

 
Thanks for reading.




Please use ideone or C++ Shell or any other online compiler link to post code in comments.
Like us? Please spread the word and help us grow. Happy coding 🙂
 





Leave a Reply

Notify of
avatar
wpDiscuz