 
 Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program to Sort a Map By Values
Java HashMap is a hash table-based implementation of Java's Map interface. It is a collection of key-value pairs.In this article, we will learn to sort a map by values in Java.
Following are the ways to sort a map by values in Java:
- Using Comparator
- Using Streams
Using Comparator
We can sort a map by values using a custom comparator. The comparator will compare the values of the map entries and sort them accordingly.
Steps to sort a map by values using Comparator:
- Step 1: Create a HashMap and populate it with some key-value pairs.
- Step 2: Convert the map entries into a list using map.entrySet().
- Step 3: Sort the list of entries using Collections.sort() with a custom comparator that compares the values of the entries.
- Step 4: Create a new LinkedHashMap and put the sorted entries into it to maintain the order.
Code to sort a map by values using Comparator:
import java.util.*;
public class SortMapByValues {
   public static void main(String[] args){
      Map<String, Integer> inputMap = new HashMap<>();
      inputMap.put("Apple", 3);
      inputMap.put("Banana", 1);
      inputMap.put("Cherry", 2);
      inputMap.put("Date", 8);
      System.out.println("Original Map: " + inputMap);
      List> list = new ArrayList<>(inputMap.entrySet());
      list.sort(Map.Entry.comparingByValue());
      LinkedHashMap<String, Integer> sortedMap = new LinkedHashMap<>();
      for (Map.Entry<String, Integer> entry : list) {
         sortedMap.put(entry.getKey(), entry.getValue());
      }
      System.out.println("Sorted Map by Values: " + sortedMap);
   }
}
Output
Following is the output of the above code:
Original Map: {Apple=3, Banana=1, Cherry=2, Date=8}
Sorted Map by Values: {Banana=1, Cherry=2, Apple=3, Date=8}
Using Streams
Java Streams is also useful for sorting a map by values. We can use methods like stream() to convert the map entries into a stream, sort them by value using a comparator, and then collect the sorted entries into a new map.
Code to sort a map by values using Streams:
import java.util.*;
import java.util.stream.Collectors;
public class SortMapByValuesUsingStreams {
   public static void main(String[] args) {
      Map<String, Integer> inputMap = new HashMap<>();
      inputMap.put("Apple", 3);
      inputMap.put("Banana", 1);
      inputMap.put("Cherry", 2);
      inputMap.put("Date", 8);
      System.out.println("Original Map: " + inputMap);
      Map<String, Integer> sortedMap = inputMap.entrySet()
         .stream()
         .sorted(Map.Entry.comparingByValue())
         .collect(Collectors.toMap(
            Map.Entry::getKey,
            Map.Entry::getValue,
            (e1, e2) -> e1,
            LinkedHashMap::new
         ));
      System.out.println("Sorted Map by Values: " + sortedMap);
   }
}
Output
When you run the above code, the output will be:
Original Map: {Apple=3, Banana=1, Cherry=2, Date=8}
Sorted Map by Values: {Banana=1, Cherry=2, Apple=3, Date=8}
Advertisements
                    