How to Sort HashMap by Value in Java - Simple Guide
To sort a
HashMap by its values in Java, convert the entries to a stream, sort them by value using Comparator, and collect the results into a LinkedHashMap to preserve order. This approach keeps the map sorted by values while maintaining key-value pairs.Syntax
Use Java Streams to sort a HashMap by values and collect the sorted entries into a LinkedHashMap to keep the order.
map.entrySet().stream(): Converts map entries to a stream.sorted(Map.Entry.comparingByValue()): Sorts entries by their values.collect(Collectors.toMap()): Collects sorted entries into a new map.LinkedHashMap::new: Keeps insertion order in the new map.
java
Map<K, V> sortedMap = map.entrySet() .stream() .sorted(Map.Entry.comparingByValue()) .collect(Collectors.toMap( Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new ));
Example
This example shows how to sort a HashMap of strings and integers by its values in ascending order and print the sorted map.
java
import java.util.*; import java.util.stream.*; public class SortHashMapByValue { public static void main(String[] args) { Map<String, Integer> map = new HashMap<>(); map.put("apple", 40); map.put("banana", 10); map.put("orange", 30); map.put("mango", 20); Map<String, Integer> sortedMap = map.entrySet() .stream() .sorted(Map.Entry.comparingByValue()) .collect(Collectors.toMap( Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new )); sortedMap.forEach((key, value) -> System.out.println(key + " = " + value)); } }
Output
banana = 10
mango = 20
orange = 30
apple = 40
Common Pitfalls
Common mistakes when sorting a HashMap by value include:
- Trying to sort the
HashMapdirectly, which is unordered by nature. - Using a
TreeMapwhich sorts by keys, not values. - Not collecting the sorted stream into a
LinkedHashMap, losing the order.
Always collect into a LinkedHashMap to preserve the sorted order.
java
/* Wrong: Sorting HashMap directly (won't work) */ // map.sort(...) - HashMap has no sort method /* Wrong: Using TreeMap (sorts by keys) */ // Map<String, Integer> treeMap = new TreeMap<>(map); /* Right: Collect sorted entries into LinkedHashMap */ Map<String, Integer> sortedMap = map.entrySet() .stream() .sorted(Map.Entry.comparingByValue()) .collect(Collectors.toMap( Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new ));
Quick Reference
Tips for sorting a HashMap by value:
- Use
entrySet().stream()to work with map entries. - Sort with
Map.Entry.comparingByValue()for natural order. - Use
LinkedHashMapto keep the sorted order. - Handle duplicate values with merge function in
Collectors.toMap().
Key Takeaways
Use Java Streams to sort a HashMap by its values efficiently.
Collect sorted entries into a LinkedHashMap to preserve order.
HashMap itself does not maintain order, so sorting requires conversion.
Avoid using TreeMap for value sorting as it sorts by keys only.
Always handle duplicate values with a merge function in collectors.