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:
We use the
entrySet()method to obtain a set of entries from the HashMap.We use the
stream()method to convert the set of entries into a stream of entries.We use the
sorted()method to sort the entries based on their values using thecomparingByValue()method.We use the
forEachOrdered()method to insert the sorted entries into a newLinkedHashMap, preserving the order of insertion.Finally, we print the sorted entries using the
forEach()method and lambda expressions.
Approach 5
In this approach:
We use the
entrySet()method to obtain a set of entries from the HashMap.We use the
stream()method to convert the set of entries into a stream of entries.We use the
sorted()method to sort the entries based on their values using thecomparingByValue()method.We use the
forEachOrdered()method to insert the sorted entries into a newLinkedHashMap, preserving the order of insertion.Finally, we print the sorted entries using the
forEach()method and lambda expressions.
Approach 6
In this approach:
We create a custom
ValueComparatorclass that implements theComparatorinterface to compare values in the HashMap.We create a TreeMap and pass an instance of the
ValueComparatorclass to its constructor. This TreeMap will sort entries based on values.We put all entries from the HashMap to the TreeMap using the
putAll()method, which automatically sorts them based on the custom comparator.Finally, we print the sorted entries.
Last updated
Was this helpful?