Sort Map by Value

Write a Java program that sorts the entries of a HashMap based on their values. Initialize a HashMap with string keys and integer values. Sort the entries based on values in ascending order and print the sorted entries.

Approach 1

import java.util.*;

public class Main {
    public static void main(String[] args) {
        // Initialize a HashMap with string keys and integer values
        HashMap<String, Integer> hashMap = new HashMap<>();
        hashMap.put("A", 11);
        hashMap.put("B", 10);
        hashMap.put("C", 9);

        // Sort the entries based on values in ascending order
        List<Map.Entry<String, Integer>> entryList = new ArrayList<>(hashMap.entrySet());
        entryList.sort(Map.Entry.comparingByValue());

        // Print the sorted entries
        System.out.println("Sorted Entries:");
        for (Map.Entry<String, Integer> entry : entryList) {
            System.out.println(entry.getKey() + ": " + entry.getValue());
        }
    }
}

This program initializes a HashMap with string keys and integer values, then sorts the entries based on their values in ascending order using the Collections.sort() method with a custom comparator. Finally, it prints the sorted entries.

Approach 2

In this approach, a TreeMap is used to sort the entries based on their values (now keys in the TreeMap). The values in the original HashMap become keys in the TreeMap, and the corresponding keys in the original HashMap become values in the TreeMap. This allows TreeMap's natural ordering to sort the entries based on their values. Finally, it prints the sorted entries.

Approach 3

In this approach, we use streams to sort the entries based on values. We first create a stream from the entry set of the HashMap, then use the sorted() method with a comparator that compares entries by their values. Finally, we collect the sorted entries into a LinkedHashMap, preserving the order of insertion, and print the sorted entries.

Approach 4

In this approach:

  1. We use the entrySet() method to obtain a set of entries from the HashMap.

  2. We use the stream() method to convert the set of entries into a stream of entries.

  3. We use the sorted() method to sort the entries based on their values using the comparingByValue() method.

  4. We use the forEachOrdered() method to insert the sorted entries into a new LinkedHashMap, preserving the order of insertion.

  5. Finally, we print the sorted entries using the forEach() method and lambda expressions.

Approach 5

In this approach:

  1. We use the entrySet() method to obtain a set of entries from the HashMap.

  2. We use the stream() method to convert the set of entries into a stream of entries.

  3. We use the sorted() method to sort the entries based on their values using the comparingByValue() method.

  4. We use the forEachOrdered() method to insert the sorted entries into a new LinkedHashMap, preserving the order of insertion.

  5. Finally, we print the sorted entries using the forEach() method and lambda expressions.

Approach 6

In this approach:

  1. We create a custom ValueComparator class that implements the Comparator interface to compare values in the HashMap.

  2. We create a TreeMap and pass an instance of the ValueComparator class to its constructor. This TreeMap will sort entries based on values.

  3. We put all entries from the HashMap to the TreeMap using the putAll() method, which automatically sorts them based on the custom comparator.

  4. Finally, we print the sorted entries.

Last updated

Was this helpful?