Sort Map by key/value using Stream APIs

Category : Java | Sub Category : Java 8 Features | By Prasad Bonam Last updated: 2020-12-10 10:47:01 Viewed : 547


Sort Map by key/value  using Stream APIs

1.
·        <String, Integer> Comparator<Entry<String, Integer>>   java.util.Map.Entry.comparingByValue() 

Returns a comparator that compares Map.Entry in natural order on value.

The returned comparator is serializable and throws NullPointerException when comparing an entry with null values.

·        <String, Integer> Comparator<Entry<String, Integer>> java.util.Map.Entry.comparingByKey(Comparator<? super String> cmp) 

Returns a comparator that compares Map.Entry by key using the given Comparator.

The returned comparator is serializable if the specified comparator is also serializable. 


·        <String> Comparator<String> java.util.Comparator.reverseOrder()

Returns a comparator that imposes the reverse of the natural ordering.

The returned comparator is serializable and throws NullPointerException when comparing null.

2. Example:

 

MapSorting.java

 

 package javaprograms;

 

import java.util.Comparator;

import java.util.HashMap;

import java.util.LinkedHashMap;

import java.util.Map;

 

public class MapSorting {

 

     public static void main(String[] args) {

         Map<String, Integer> unSortedMap = new HashMap<>();

         unSortedMap.put("Abhi", 5);

         unSortedMap.put("Ram", 1);

         unSortedMap.put("Smita", 3);

         unSortedMap.put("Yuva", 4);

         unSortedMap.put("Bowen", 2);

 

         System.out.println("unSortedMap ==> " + unSortedMap);



                          /** ascending order by value **/

         LinkedHashMap<String, Integer> sortedMap = new LinkedHashMap<>();        

     unSortedMap.entrySet().stream().sorted(Map.Entry.comparingByValue())

                  .forEachOrdered(x -> sortedMap.put(x.getKey(), x.getValue()));

 

         System.out.println("In ascending order by value   ==>" + sortedMap);


 


         /** descending order by value **/

         LinkedHashMap<String, Integer> reverseSortedMap = new LinkedHashMap<>(); 

      unSortedMap.entrySet().stream().sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))

                  .forEachOrdered(x -> reverseSortedMap.put(x.getKey(), x.getValue()));

 

         System.out.println("In descending order by value ==>" + reverseSortedMap);


 


         /** ascending order by key **/

         LinkedHashMap<String, Integer> sortedMapKey = new LinkedHashMap<>();

     unSortedMap.entrySet().stream().sorted(Map.Entry.comparingByKey())

                  .forEachOrdered(x -> sortedMapKey.put(x.getKey(), x.getValue()));

 

         System.out.println("In ascending order by key   ==>" + sortedMapKey);


 


         /** descending order by key**/

         LinkedHashMap<String, Integer> reverseSortedMapKey = new LinkedHashMap<>();

       unSortedMap.entrySet().stream().sorted(Map.Entry.comparingByKey(Comparator.reverseOrder()))

                  .forEachOrdered(x -> reverseSortedMapKey.put(x.getKey(), x.getValue()));

 

         System.out.println("In descending order by key   ==>" + reverseSortedMapKey);

 

     }

 

}

 


Output:

unSortedMap ==>

 {Bowen=2, Smita=3, Abhi=5, Yuva=4, Ram=1}

 

In ascending order by value   ==>

{Ram=1, Bowen=2, Smita=3, Yuva=4, Abhi=5}

 

In descending order by value ==>

{Abhi=5, Yuva=4, Smita=3, Bowen=2, Ram=1}

 

In ascending order by key   ==>

{Abhi=5, Bowen=2, Ram=1, Smita=3, Yuva=4}

 

In descending order by key   ==>

{Yuva=4, Smita=3, Ram=1, Bowen=2, Abhi=5}

Search
Related Articles

Leave a Comment: