Java Program to Sort a Map By Values

Java Program to Sort a Map By Values

Java Map Interface

The Map interface also provides methods for checking the presence of a key, getting the size of the Map, and iterating over the Map to process its key-value pairs.

  • Map is a generic interface, declared as Map<K, V>.
  • A Map cannot contain duplicate keys. Each key can map to at most one value.

To know more about Java Program to Sort map by Values read the complete article.

Steps for Program to Sort a Map By Values:

  • Convert the Map into a Stream.
  • Sort the stream using the sorted method with a custom Comparator that compares the Map values.
  • Collect the sorted stream into a new Map using the collect method and toMap collector with the original Map’s keys and values.
  • Return the newly sorted Map.

Let’s look at a Java Program to Sort map by Values to perform certain operations .

Example 1: Java Program to Sort a Map By Values

Run

import java.util.*;

public class Main {
  public static void main(String[] args) {
    Map<String, Integer> map = new HashMap<>();
    map.put("Ashish", 3);
    map.put("Ananya", 2);
    map.put("Ayush", 1);
    map.put("Aryan", 4);

    List<Map.Entry<String, Integer>> list = new ArrayList<>(map.entrySet());
    list.sort(Map.Entry.comparingByValue());

    System.out.println("Sorted Map by Values: " + list);
  }
}

Output

Sorted Map by Values: [Ayush=1, Ananya=2, Ashish=3, Aryan=4]

Example 2 :Java Program to Sort a Map By Values

Run

import java.util.*;

public class Main {
  public static void main(String[] args) {
    Map<String, Integer> map = new HashMap<>();
    map.put("Hyundai", 15);
    map.put("Maruti", 40);
    map.put("Honda", 10);
    map.put("Tata", 25);
    map.put("Kia", 5);
    map.put("Mahindra", 8);


    List<Map.Entry<String, Integer>> list = new ArrayList<>(map.entrySet());
    list.sort(Map.Entry.comparingByValue());

    System.out.println("Sorted Map by Values: " + list);
  }
}

Output

Sorted Map by Values: [Kia=5, Mahindra=8, Honda=10, Hyundai=15, Tata=25, Maruti=40]

Example 3: Java Program to sort a map by Values.

Run

import java.util.*;

public class Main {
  public static void main(String[] args) {
    Map<String, Integer> map = new HashMap<>();
    map.put("Alex", 50);
    map.put("Bob", 60);
    map.put("Charlie", 70);
    map.put("David", 80);

    List<Map.Entry<String, Integer>> list = new ArrayList<>(map.entrySet());
    list.sort(Map.Entry.<String, Integer>comparingByValue().reversed());

    System.out.println("Sorted Map by Values in Descending Order: " + list);
  }
}

Output

Sorted Map by Values in Descending Order: [David=80, Charlie=70, Bob=60, Alex=50]

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription