Java 8 Sort Map by Value

In this article we are going to see about Java 8 – How to Sort map by value Example

Java8SortMap.java

package com.ehowtonow.java8;

import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.stream.Collectors;

public class Java8SortMap {

 public static void main(String[] args) {
  Map batsmanRanking = new HashMap();
  batsmanRanking.put("Virat Kohli", 1);
  batsmanRanking.put("AB de Villiers", 2);
  batsmanRanking.put("David Warner", 3);
  batsmanRanking.put("Joe Root", 4);
  batsmanRanking.put("Kane Williamson", 5);
  batsmanRanking.put("Quinton de Kock", 6);
  batsmanRanking.put("Faf du Plessis", 7);
  batsmanRanking.put("Babar Azam", 8);
  batsmanRanking.put("Martin Guptill", 9);
  batsmanRanking.put("Shikhar Dhawan", 10);
  
  System.out.println("Unsort Map...");
        System.out.println(batsmanRanking);
        
        //Convert a Map into a Stream
        //Sort it - values, reserve it and return a new LinkedHashMap
        //Collect and return a new LinkedHashMap (keep the order) - 
        //toMap() will returns HashMap by default, we need LinkedHashMap to keep the order.
        Map resultMap1 = batsmanRanking.entrySet().stream()
                .sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
                .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,
                        (oldValue, newValue) -> oldValue, LinkedHashMap::new));
        
        Map resultMap2 = new LinkedHashMap<>();
        batsmanRanking.entrySet().stream()
        .sorted(Map.Entry.comparingByValue().reversed())
        .forEachOrdered(x -> resultMap2.put(x.getKey(), x.getValue()));

        System.out.println("Sorted Map 1...");
        System.out.println(resultMap1);
        System.out.println("Sorted Map 2...");
        System.out.println(resultMap2);
 }

}

Output
Unsort Map…
{Faf du Plessis=7, Virat Kohli=1, Martin Guptill=9, Babar Azam=8, Quinton de Kock=6, Kane Williamson=5, AB de Villiers=2, Joe Root=4, David Warner=3, Shikhar Dhawan=10}
Sorted Map 1…
{Shikhar Dhawan=10, Martin Guptill=9, Babar Azam=8, Faf du Plessis=7, Quinton de Kock=6, Kane Williamson=5, Joe Root=4, David Warner=3, AB de Villiers=2, Virat Kohli=1}
Sorted Map 2…
{Shikhar Dhawan=10, Martin Guptill=9, Babar Azam=8, Faf du Plessis=7, Quinton de Kock=6, Kane Williamson=5, Joe Root=4, David Warner=3, AB de Villiers=2, Virat Kohli=1}


Ask your questions in eHowToNow Forum

Post your technical, non-technical doubts, questions in our site. Get answer as soon as possible, meanwhile you can help others by answering, unanswered questions.
To Ask new Question : Ask Question
Check our existing discussions : Questions & Answers

  1. java.util.stream Package
  2. Java – Consumer Interface
  3. Java – IntFunction Interface
  4. Java 8 Functional Interface
  5. Java – Collectors Class
  6. Java – DoubleToLongFunction Interface
  7. Java 8 New Features Overview
  8. Java – LongUnaryOperator Interface
  9. Java – LongToDoubleFunction Interface
  10. Java 8 Overview and New Features
  11. Java – ToIntBiFunction Interface
  12. Java – DoubleFunction Interface
  13. Java – ObjIntConsumer Interface
  14. Java 8 Sort Map by Key
  15. Java 8 – filter the null values from Stream Example

Be the first to comment

Leave a Reply