In this article, we will discuss how to sort a HashMap by its Values first and then by its Keys
1. Sorting HashMap by Value first and then by Key :
- Here, Map Values will be sorted first in decreasing-order and then by Keys in decreasing-order
- While sorting if there are 2/more same Map Values then sort according to Keys
SortHashMapByValuesAndThenByKeys.java
package in.bench.resources.find.duplicate.count;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.stream.Collectors;
public class SortHashMapByValuesAndThenByKeys {
public static void main(String[] args) {
// create HashMap object
Map<Integer, Long> counts = new HashMap<>();
// add entries to HashMap
counts.put(7, 2L);
counts.put(11, 2L);
counts.put(8, 1L);
counts.put(2, 1L);
counts.put(10, 1L);
counts.put(5, 3L);
counts.put(9, 5L);
// sort HashMap by Values in decreasing-order & Keys in decreasing-order
LinkedHashMap<Integer, Long> sortedMap = counts
.entrySet()
.stream()
.sorted(
Map.Entry.<Integer, Long>comparingByValue().reversed()
.thenComparing(Map.Entry.<Integer, Long>comparingByKey().reversed())
)
.collect(
Collectors.toMap(
Map.Entry::getKey,
Map.Entry::getValue,
(oldValue, newValue) -> oldValue,
LinkedHashMap::new)
);
// print to console
System.out.println("Value\tKey\n");
sortedMap.forEach((key, value) -> System.out.println(value + "\t" + key));
}
}
Output :
Value Key
5 9
3 5
2 11
2 7
1 10
1 8
1 2
2. Sorting in Natural order :
- Here again Map Values will be sorted first and then Keys but both in increasing-order (or ascending-order)
SortHashMapByValuesAndThenByKeys.java
// sort HashMap by Values in increasing-order & Keys in increasing-order
LinkedHashMap<Integer, Long> sortedMap = counts
.entrySet()
.stream()
.sorted(
Map.Entry.<Integer, Long>comparingByValue()
.thenComparing(Map.Entry.<Integer, Long>comparingByKey())
)
.collect(
Collectors.toMap(
Map.Entry::getKey,
Map.Entry::getValue,
(oldValue, newValue) -> oldValue,
LinkedHashMap::new)
);
3. Different order for Map Values & Keys :
- Sorting Map Values in ascending-order and Keys in descending-order
SortHashMapByValuesAndThenByKeys.java
// sort HashMap by Values in increasing-order & Keys in decreasing-order
LinkedHashMap<Integer, Long> sortedMap = counts
.entrySet()
.stream()
.sorted(
Map.Entry.<Integer, Long>comparingByValue()
.thenComparing(Map.Entry.<Integer, Long>comparingByKey().reversed())
)
.collect(
Collectors.toMap(
Map.Entry::getKey,
Map.Entry::getValue,
(oldValue, newValue) -> oldValue,
LinkedHashMap::new)
);
- Sorting Map Values in descending-order and Keys in ascending-order
SortHashMapByValuesAndThenByKeys.java
// sort HashMap by Values in decreasing-order & Keys in increasing-order
LinkedHashMap<Integer, Long> sortedMap = counts
.entrySet()
.stream()
.sorted(
Map.Entry.<Integer, Long>comparingByValue().reversed()
.thenComparing(Map.Entry.<Integer, Long>comparingByKey())
)
.collect(
Collectors.toMap(
Map.Entry::getKey,
Map.Entry::getValue,
(oldValue, newValue) -> oldValue,
LinkedHashMap::new)
);
4. Trivia :
- There are different ways to sort Map
- Map.Entry.comparingKey() or Map.Entry.comparingValue() are the APIs (or methods) provided in Java 1.8 version
- But there are many other ways to sort Map Values & Keys
- Read below article to get insight into that techniques
Related Articles :
- Java 8 – How to Sort HashMap Key-Value pairs by its Key in ascending & descending order ?
- Java 8 – How to Sort HashMap Key-Value pairs by its Value in ascending & descending order ?
- Java 8 – Sorting HashMap entries by its Key and Value
- Java 8 – How to Sort a Map entries by its Key – 6 ways ?
- Java 8 – How to Sort a Map entries by its Value – 6 ways ?
- Java 8 – How to sort LinkedHashMap entries by its Key in ascending/descending order ?
- Java 8 – How to sort LinkedHashMap entries by its Value in ascending/descending order ?
- Java 8 – How to sort TreeMap entries in descending order ?
- Java 8 – How to sort HashMap by its Values first and then by its Keys ?
References:
- https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html
- https://docs.oracle.com/javase/8/docs/api/java/util/stream/Collectors.html
- https://docs.oracle.com/javase/8/docs/api/java/util/Map.html
- https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html
- https://docs.oracle.com/javase/8/docs/api/java/util/LinkedHashMap.html
Happy Coding !!
Happy Learning !!