In this article, we will discuss how to sort HashMap entries by its Values in ascending & descending order using Java 8 Stream‘s sorted() method and Comparator
Before proceeding with this sorting examples understand below items,
- What is Map ?
- What is Map.Entry ?
- HashMap and its properties
- How to iterate Map ?
- Sorting Map by its Key – before Java 8
- Sorting Map by its Value – before Java 8
1. Map.Entry.comparingByValue() method
- Returns a comparator that compares
Map.Entry
in natural order on value - The returned comparator is serializable and throws
NullPointerException
when comparing an entry with null values - Method signature :- static <K,V extends Comparable<? super V>> Comparator<Map.Entry<K,V>> comparingByValue()
1.1 Sorting HashMap by its Value in Ascending order
- A HashMap contains 7 entries with Values being in Integer type
- We are sorting these Map Values in natural order using Java 8 Stream’s sorted() method and passing Map.Entry.comparingByValue() comparator as argument to sorted() method
SortHashMapByValuesInAscending.java
package net.bench.resources.stream.sort.hashmap;
import java.util.HashMap;
import java.util.Map;
public class SortHashMapByValuesInAscending {
public static void main(String[] args) {
// 1. creating HashMap object of type <String, Integer>
Map<String, Integer> mnc = new HashMap<>();
// 1.1 adding key-value pairs to HashMap object
mnc.put("HCL", 3);
mnc.put("LTI", 7);
mnc.put("Cogni", 6);
mnc.put("TCS", 1);
mnc.put("TechM", 5);
mnc.put("Infy", 2);
mnc.put("Wipro", 4);
// 1.2 print - before sorting - random order
System.out.println("Before Sorting :- \n");
// 1.3 print Map entries to console
mnc.forEach((key, value) -> System.out.println(
"Key : " + key + "\t\t" + "Value : " + value
));
// 2. Sorting according to natural order of Values
System.out.println("\n\nSorting according to natural order of Values :- \n");
// 2.1 sorting by Map values in ascending order
mnc
.entrySet()
.stream()
.sorted(Map.Entry.comparingByValue())
.forEach(entry -> System.out.println(
"Key : " + entry.getKey() + "\t\t" + "Value : " + entry.getValue()
));
}
}
Output:
Before Sorting :-
Key : HCL Value : 3
Key : LTI Value : 7
Key : Wipro Value : 4
Key : Cogni Value : 6
Key : TCS Value : 1
Key : TechM Value : 5
Key : Infy Value : 2
Sorting according to natural order of Values :-
Key : TCS Value : 1
Key : Infy Value : 2
Key : HCL Value : 3
Key : Wipro Value : 4
Key : TechM Value : 5
Key : Cogni Value : 6
Key : LTI Value : 7
2. Map.Entry.comparingByValue(Comparator) method
- Returns a comparator that compares
Map.Entry
by value using the givenComparator
- The returned comparator is serializable if the specified comparator is also serializable
- Method signature :- static <K,V> Comparator<Map.Entry<K,V>> comparingByValue(Comparator<? super V> cmp)
2.1 Sorting HashMap by its Key in Descending order
- A HashMap contains 7 entries with Values being in Integer type
- We are sorting these Map Values in reverse order using Java 8 Stream’s sorted() method and passing Map.Entry.comparingByValue(Comparator.reverseOrder()) comparator as argument to sorted() method
SortHashMapByValuesInDescending.java
package net.bench.resources.stream.sort.hashmap;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
public class SortHashMapByValuesInDescending {
public static void main(String[] args) {
// 1. creating HashMap object of type <String, Integer>
Map<String, Integer> mnc = new HashMap<>();
// 1.1 adding key-value pairs to HashMap object
mnc.put("HCL", 3);
mnc.put("LTI", 7);
mnc.put("Cogni", 6);
mnc.put("TCS", 1);
mnc.put("TechM", 5);
mnc.put("Infy", 2);
mnc.put("Wipro", 4);
// 1.2 print - before sorting - random order
System.out.println("Before Sorting :- \n");
// 1.3 print Map entries to console
mnc.forEach((key, value) -> System.out.println(
"Key : " + key + "\t\t" + "Value : " + value
));
// 2. Sorting according to reverse order of Values
System.out.println("\n\nSorting according to reverse order of Values :- \n");
// 2.1 sorting by Map values in descending order
mnc
.entrySet()
.stream()
.sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
.forEach(entry -> System.out.println(
"Key : " + entry.getKey() + "\t\t" + "Value : " + entry.getValue()
));
}
}
Output:
Before Sorting :-
Key : HCL Value : 3
Key : LTI Value : 7
Key : Wipro Value : 4
Key : Cogni Value : 6
Key : TCS Value : 1
Key : TechM Value : 5
Key : Infy Value : 2
Sorting according to reverse order of Values :-
Key : LTI Value : 7
Key : Cogni Value : 6
Key : TechM Value : 5
Key : Wipro Value : 4
Key : HCL Value : 3
Key : Infy Value : 2
Key : TCS Value : 1
3. Important points to remember about Map :
- HashMap stores entries (Key-Value pairs) in random-order of Keys
- LinkedHashMap stores entries (Key-Value pairs) as per insertion-order of Keys
- TreeMap stores entries (Key-Value pairs) in sorted-order of Keys
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
References:
- https://docs.oracle.com/javase/8/docs/api/java/util/Map.html#entrySet–
- https://docs.oracle.com/javase/8/docs/api/java/util/Map.Entry.html
- https://docs.oracle.com/javase/8/docs/api/java/util/Map.Entry.html#comparingByValue–
- https://docs.oracle.com/javase/8/docs/api/java/util/Map.Entry.html#comparingByValue-java.util.Comparator-
- https://docs.oracle.com/javase/8/docs/api/java/util/Map.Entry.html#comparingByKey–
- https://docs.oracle.com/javase/8/docs/api/java/util/Map.Entry.html#comparingByKey-java.util.Comparator-
- https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html
- https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html#sorted–