In this article, we will discuss different ways to sort a Map entries (key-value pairs) by its Key
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
Different approaches of Sorting a Map by its Key :
- TreeMap class
- ArrayList class and Collections.sort() method
- TreeSet class
- Java 8 – Lambda function
- Java 8 – Stream sorted() method
- Java 8 – Map.Entry.comparingByKey() comparator
1. Using TreeMap class
- In this approach, we are going to use inter-conversion constructor between HashMap & TreeMap for natural order sorting i.e.; alphabetical order of Keys which is in String type
- For reverse order sorting, create another TreeMap object passing Collections.reverseOrder() comparator as constructor-argument and put all HashMap entries to TreeMap using putAll() method
SortMapByKeysUsingTreeMap.java
package net.bench.resources.map.keys.sorting.ways;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
public class SortMapByKeysUsingTreeMap {
public static void main(String[] args) {
// 1. creating HashMap object of type <String, Integer>
Map<String, Integer> countryPopulation = new HashMap<>();
// 1.1 adding key-value pairs to HashMap object
countryPopulation.put("Indian", 382357386);
countryPopulation.put("America", 332429717);
countryPopulation.put("Russia", 146748590);
countryPopulation.put("Brazil", 213728559);
countryPopulation.put("Pakistan", 220892331);
// 1.2 print - before sorting - random order
System.out.println("Before Sorting - Random order :- \n");
// 1.3 print Map entries to console
countryPopulation.forEach((key, value) -> System.out.println(
"Key : " + key + "\t\t" + "Value : " + value
));
// 2. Sorting according to alphabetical order of Keys
System.out.println("\n\nSorted according to "
+ "alphabetical order of Keys :- \n");
// 2.1 sorting by Map keys in ascending order
Map<String, Integer> sortedMapAsc = new TreeMap<>(countryPopulation);
// 2.2 print Map entries to console
sortedMapAsc.forEach((key, value) -> System.out.println(
"Key : " + key + "\t\t" + "Value : " + value
));
// 3. Sorting according to reverse alphabetical order of Keys
System.out.println("\n\nSorted according to "
+ "reverse alphabetical order of Keys :- \n");
// 3.1 sorting by Map keys in descending order
Map<String, Integer> sortedMapDesc = new TreeMap<>(
Collections.reverseOrder());
// 3.2 add unsorted map to TreeMap for reverse-order sorting
sortedMapDesc.putAll(countryPopulation);
// 3.2 print Map entries to console
sortedMapDesc.forEach((key, value) -> System.out.println(
"Key : " + key + "\t\t" + "Value : " + value
));
}
}
Output:
Before Sorting - Random order :-
Key : Pakistan Value : 220892331
Key : America Value : 332429717
Key : Brazil Value : 213728559
Key : Indian Value : 382357386
Key : Russia Value : 146748590
Sorted according to alphabetical order of Keys :-
Key : America Value : 332429717
Key : Brazil Value : 213728559
Key : Indian Value : 382357386
Key : Pakistan Value : 220892331
Key : Russia Value : 146748590
Sorted according to reverse alphabetical order of Keys :-
Key : Russia Value : 146748590
Key : Pakistan Value : 220892331
Key : Indian Value : 382357386
Key : Brazil Value : 213728559
Key : America Value : 332429717
2. Using ArrayList class & Collections.sort() method
- In this approach, first we will get key set from HashMap entries and pass this key-set as constructor-argument while creating ArrayList object and use Collections.sort() method for natural order sorting i.e.; alphabetical order of Keys which is in String type
- For reverse order sorting, use same ArrayList and while sorting using Collections.sort() method and pass Collections.reverseOrder() comparator as 2nd argument
SortMapByKeysUsingArrayList.java
package net.bench.resources.map.keys.sorting.ways;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class SortMapByKeysUsingArrayList {
public static void main(String[] args) {
// 1. creating HashMap object of type <String, Integer>
Map<String, Integer> countryPopulation = new HashMap<>();
// 1.1 adding key-value pairs to HashMap object
countryPopulation.put("Indian", 382357386);
countryPopulation.put("America", 332429717);
countryPopulation.put("Russia", 146748590);
countryPopulation.put("Brazil", 213728559);
countryPopulation.put("Pakistan", 220892331);
// 1.2 print - before sorting - random order
System.out.println("Before Sorting - Random order :- \n");
// 1.3 print Map entries to console
countryPopulation.forEach((key, value) -> System.out.println(
"Key : " + key + "\t\t" + "Value : " + value
));
// 2. Sorting according to alphabetical order of Keys
System.out.println("\n\nSorted according to "
+ "alphabetical order of Keys :- \n");
// 2.1 sorting by Map keys in ascending order
List<String> mapKeys = new ArrayList<>(countryPopulation.keySet());
// 2.2 Map keys - Ascending order sorting ArrayList
Collections.sort(mapKeys);
// 2.3 print Map entries for ascending order
mapKeys.forEach(key -> System.out.println(
"Key : " + key + "\t\t" +
"Value : " + countryPopulation.get(key)
));
// 3. Sorting according to reverse alphabetical order of Keys
System.out.println("\n\nSorted according to "
+ "reverse alphabetical order of Keys :- \n");
// 3.1 Map keys - Descending order sorting ArrayList
Collections.sort(mapKeys, Collections.reverseOrder());
// 3.2 print Map entries for descending order
mapKeys.forEach(key -> System.out.println(
"Key : " + key + "\t\t" +
"Value : " + countryPopulation.get(key)
));
}
}
Output:
Before Sorting - Random order :-
Key : Pakistan Value : 220892331
Key : America Value : 332429717
Key : Brazil Value : 213728559
Key : Indian Value : 382357386
Key : Russia Value : 146748590
Sorted according to alphabetical order of Keys :-
Key : America Value : 332429717
Key : Brazil Value : 213728559
Key : Indian Value : 382357386
Key : Pakistan Value : 220892331
Key : Russia Value : 146748590
Sorted according to reverse alphabetical order of Keys :-
Key : Russia Value : 146748590
Key : Pakistan Value : 220892331
Key : Indian Value : 382357386
Key : Brazil Value : 213728559
Key : America Value : 332429717
3. Using TreeSet class
- In this approach, first we will get key set from HashMap entries and pass this key-set as constructor-argument while creating TreeSet object which will sort keys in natural order i.e.; alphabetical order of Keys which is in String type
- For reverse order sorting, create another TreeSet object passing Collections.reverseOrder() comparator as constructor-argument and add all Key-set to TreeSet using addAll() method
- Note: alternatively we can provide our own customized logic for reverse-order sorting by overriding compare() method of Comparator interface which is shown in the below illustration
SortMapByKeysUsingTreeSet.java
package net.bench.resources.map.keys.sorting.ways;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.SortedSet;
import java.util.TreeSet;
public class SortMapByKeysUsingTreeSet {
public static void main(String[] args) {
// 1. creating HashMap object of type <String, Integer>
Map<String, Integer> countryPopulation = new HashMap<>();
// 1.1 adding key-value pairs to HashMap object
countryPopulation.put("Indian", 382357386);
countryPopulation.put("America", 332429717);
countryPopulation.put("Russia", 146748590);
countryPopulation.put("Brazil", 213728559);
countryPopulation.put("Pakistan", 220892331);
// 1.2 print - before sorting - random order
System.out.println("Before Sorting - Random order :- \n");
// 1.3 print Map entries to console
countryPopulation.forEach((key, value) -> System.out.println(
"Key : " + key + "\t\t" + "Value : " + value
));
// 2. Sorting according to alphabetical order of Keys
System.out.println("\n\nSorted according to "
+ "alphabetical order of Keys :- \n");
// 2.1 TreeSet - Ascending order Sorting by Map keys
SortedSet<String> mapKeysAsc = new TreeSet<>(
countryPopulation.keySet());
// 2.2 print Map entries
mapKeysAsc.forEach(key -> System.out.println(
"Key : " + key + "\t\t" +
"Value : " + countryPopulation.get(key)
));
// 3. Sorting according to reverse alphabetical order of Keys
System.out.println("\n\nSorted according to "
+ "reverse alphabetical order of Keys :- \n");
// 3.1 TreeSet - Descending order Sorting by Map keys
SortedSet<String> mapKeysDesc = new TreeSet<>(new Comparator<String>() {
@Override
public int compare(String str1, String str2) {
return str2.compareTo(str1);
}
});
// 3.2 add unsorted keySet to TreeSet for reverse-order sorting
mapKeysDesc.addAll(countryPopulation.keySet());
// 3.3 print Map entries
mapKeysDesc.forEach(key -> System.out.println(
"Key : " + key + "\t\t" +
"Value : " + countryPopulation.get(key)
));
}
}
Output:
Before Sorting - Random order :-
Key : Pakistan Value : 220892331
Key : America Value : 332429717
Key : Brazil Value : 213728559
Key : Indian Value : 382357386
Key : Russia Value : 146748590
Sorted according to alphabetical order of Keys :-
Key : America Value : 332429717
Key : Brazil Value : 213728559
Key : Indian Value : 382357386
Key : Pakistan Value : 220892331
Key : Russia Value : 146748590
Sorted according to reverse alphabetical order of Keys :-
Key : Russia Value : 146748590
Key : Pakistan Value : 220892331
Key : Indian Value : 382357386
Key : Brazil Value : 213728559
Key : America Value : 332429717
4. Java 8 – Lambda function
- In this approach, first we will get Map entry set from HashMap and pass this entry-set as constructor-argument while creating LinkedList object and use Collections.sort() method by passing converted LinkedList as 1st argument and below lambda expression for natural order sorting as 2nd argument
- (map1, map2) -> map1.getKey().compareTo(map2.getKey())
- For reverse order sorting, just twist the comparison logic in the lambda expression as shown below,
- (map1, map2) -> map2.getKey().compareTo(map1.getKey())
SortMapByKeysUsingJava8Lambda.java
package net.bench.resources.map.keys.sorting.ways;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
public class SortMapByKeysUsingJava8Lambda {
public static void main(String[] args) {
// 1. creating HashMap object of type <String, Integer>
Map<String, Integer> countryPopulation = new HashMap<>();
// 1.1 adding key-value pairs to HashMap object
countryPopulation.put("Indian", 382357386);
countryPopulation.put("America", 332429717);
countryPopulation.put("Russia", 146748590);
countryPopulation.put("Brazil", 213728559);
countryPopulation.put("Pakistan", 220892331);
// 1.2 print - before sorting - random order
System.out.println("Before Sorting - Random order :- \n");
// 1.3 print Map entries to console
countryPopulation.forEach((key, value) -> System.out.println(
"Key : " + key + "\t\t" + "Value : " + value
));
// 1.4 convert Map EntrySet into List
List<Map.Entry<String, Integer>> list = new LinkedList<>(
countryPopulation.entrySet());
// 2. Sorting according to alphabetical order of Keys
System.out.println("\n\nSorted according to "
+ "alphabetical order of Keys :- \n");
// 2.1 Map keys - Ascending order sorting
Collections.sort(list,
(map1, map2) -> map1.getKey().compareTo(map2.getKey())
);
// 2.2 put above sorted map into LinkedHashMap by iterating
Map<String, Integer> tempMapAsc = new LinkedHashMap<>();
// 2.3 iterate and store in newly created LinkedHashMap
for (Map.Entry<String, Integer> map : list) {
tempMapAsc.put(map.getKey(), map.getValue());
}
// 2.4 print Map entries to console
tempMapAsc.forEach((key, value) -> System.out.println(
"Key : " + key + "\t\t" + "Value : " + value
));
// 3. Sorting according to reverse alphabetical order of Keys
System.out.println("\n\nSorted according to "
+ "reverse alphabetical order of Keys :- \n");
// 3.1 Map keys - Descending order sorting
Collections.sort(list,
(map1, map2) -> map2.getKey().compareTo(map1.getKey())
);
// 3.2 put above reverse sorted map into LinkedHashMap by iterating
Map<String, Integer> tempMapDesc = new LinkedHashMap<>();
// 3.3 iterate and store in newly created LinkedHashMap
for (Map.Entry<String, Integer> map : list) {
tempMapDesc.put(map.getKey(), map.getValue());
}
// 3.4 print Map entries to console
tempMapDesc.forEach((key, value) -> System.out.println(
"Key : " + key + "\t\t" + "Value : " + value
));
}
}
Output:
Before Sorting - Random order :-
Key : Pakistan Value : 220892331
Key : America Value : 332429717
Key : Brazil Value : 213728559
Key : Indian Value : 382357386
Key : Russia Value : 146748590
Sorted according to alphabetical order of Keys :-
Key : America Value : 332429717
Key : Brazil Value : 213728559
Key : Indian Value : 382357386
Key : Pakistan Value : 220892331
Key : Russia Value : 146748590
Sorted according to reverse alphabetical order of Keys :-
Key : Russia Value : 146748590
Key : Pakistan Value : 220892331
Key : Indian Value : 382357386
Key : Brazil Value : 213728559
Key : America Value : 332429717
5. Java 8 – Stream sorted() method
- In this approach, we are going to use Stream‘s sorted() method for sorting Map Keys by passing lambda expression as argument to sorted() method
- Ascending order sorting :- for alphabetical order of keys pass below lambda expression,
- (map1, map2) -> map1.getKey().compareTo(map2.getKey())
- Descending order sorting :- for reverse alphabetical order of keys pass below lambda expression,
- (map1, map2) -> map2.getKey().compareTo(map1.getKey())
SortMapByKeysUsingStreamSortedMethod.java
package net.bench.resources.map.keys.sorting.ways;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.stream.Collectors;
public class SortMapByKeysUsingStreamSortedMethod {
public static void main(String[] args) {
// 1. creating HashMap object of type <String, Integer>
Map<String, Integer> countryPopulation = new HashMap<>();
// 1.1 adding key-value pairs to HashMap object
countryPopulation.put("Indian", 382357386);
countryPopulation.put("America", 332429717);
countryPopulation.put("Russia", 146748590);
countryPopulation.put("Brazil", 213728559);
countryPopulation.put("Pakistan", 220892331);
// 1.2 print - before sorting - random order
System.out.println("Before Sorting - Random order :- \n");
// 1.3 print Map entries to console
countryPopulation.forEach((key, value) -> System.out.println(
"Key : " + key + "\t\t" + "Value : " + value
));
// 2. Sorting according to alphabetical order of Keys
System.out.println("\n\nSorted according to "
+ "alphabetical order of Keys :- \n");
// 2.1 Stream.sorted - Ascending order of keys
Map<String, Integer> tempMapAsc = countryPopulation
.entrySet()
.stream()
.sorted(
(map1, map2) -> map1.getKey().compareTo(map2.getKey())
)
.collect(
Collectors.toMap(
Map.Entry::getKey,
Map.Entry::getValue,
(es1, es2) -> es1, LinkedHashMap::new
)
);
// 2.2 print Map entries
tempMapAsc.forEach((key, value) -> System.out.println(
"Key : " + key + "\t\t" + "Value : " + value
));
// 3. Sorting according to reverse alphabetical order of Keys
System.out.println("\n\nSorted according to "
+ "reverse alphabetical order of Keys :- \n");
// 3.1 Stream.sorted - Descending order of keys
Map<String, Integer> tempMapDesc = countryPopulation
.entrySet()
.stream()
.sorted(
(map1, map2) -> map2.getKey().compareTo(map1.getKey())
)
.collect(
Collectors.toMap(
Map.Entry::getKey,
Map.Entry::getValue,
(es1, es2) -> es1, LinkedHashMap::new
)
);
// 3.2 print Map entries
tempMapDesc.forEach((key, value) -> System.out.println(
"Key : " + key + "\t\t" + "Value : " + value
));
}
}
Output:
Before Sorting - Random order :-
Key : Pakistan Value : 220892331
Key : America Value : 332429717
Key : Brazil Value : 213728559
Key : Indian Value : 382357386
Key : Russia Value : 146748590
Sorted according to alphabetical order of Keys :-
Key : America Value : 332429717
Key : Brazil Value : 213728559
Key : Indian Value : 382357386
Key : Pakistan Value : 220892331
Key : Russia Value : 146748590
Sorted according to reverse alphabetical order of Keys :-
Key : Russia Value : 146748590
Key : Pakistan Value : 220892331
Key : Indian Value : 382357386
Key : Brazil Value : 213728559
Key : America Value : 332429717
6. Java 8 – Map.Entry.comparingByKey() comparator
- In this approach, we are going to use Stream‘s sorted() method for sorting Map Keys by passing comparator as argument to sorted() method
- For alphabetical order of keys pass Map.Entry.comparingByKey() comparator
- For reverse alphabetical order of keys pass Map.Entry.comparingByKey(Comparator.reverseOrder()) comparator
SortMapEntryUsingComparingByKey.java
package net.bench.resources.map.keys.sorting.ways;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
public class SortMapEntryUsingComparingByKey {
public static void main(String[] args) {
// 1. creating HashMap object of type <String, Integer>
Map<String, Integer> countryPopulation = new HashMap<>();
// 1.1 adding key-value pairs to HashMap object
countryPopulation.put("Indian", 382357386);
countryPopulation.put("America", 332429717);
countryPopulation.put("Russia", 146748590);
countryPopulation.put("Brazil", 213728559);
countryPopulation.put("Pakistan", 220892331);
// 1.2 print - before sorting - random order
System.out.println("Before Sorting - Random order :- \n");
// 1.3 print Map entries to console
countryPopulation.forEach((key, value) -> System.out.println(
"Key : " + key + "\t\t" + "Value : " + value
));
// 2. Sorting according to alphabetical order of Keys
System.out.println("\n\nSorted according to "
+ "alphabetical order of Keys :- \n");
// 2.1 Ascending Stream sorting using Map.Entry.comparingByKey()
countryPopulation
.entrySet()
.stream()
.sorted(Map.Entry.comparingByKey())
.forEach(entry -> System.out.println(
"Key : " + entry.getKey() + "\t\t" +
"Value : " + entry.getValue()
));
// 3. Sorting according to reverse alphabetical order of Keys
System.out.println("\n\nSorted according to "
+ "reverse alphabetical order of Keys :- \n");
// 3.1 Descending Stream sorting using Map.Entry.comparingByKey()
countryPopulation
.entrySet()
.stream()
.sorted(Map.Entry.comparingByKey(Comparator.reverseOrder()))
.forEach(entry -> System.out.println(
"Key : " + entry.getKey() + "\t\t" +
"Value : " + entry.getValue()
));
}
}
Output:
Before Sorting - Random order :-
Key : Pakistan Value : 220892331
Key : America Value : 332429717
Key : Brazil Value : 213728559
Key : Indian Value : 382357386
Key : Russia Value : 146748590
Sorted according to alphabetical order of Keys :-
Key : America Value : 332429717
Key : Brazil Value : 213728559
Key : Indian Value : 382357386
Key : Pakistan Value : 220892331
Key : Russia Value : 146748590
Sorted according to reverse alphabetical order of Keys :-
Key : Russia Value : 146748590
Key : Pakistan Value : 220892331
Key : Indian Value : 382357386
Key : Brazil Value : 213728559
Key : America Value : 332429717
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
References:
- https://docs.oracle.com/javase/8/docs/api/java/util/Collection.html
- https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.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
- https://docs.oracle.com/javase/8/docs/api/java/util/TreeMap.html
- https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html
- https://docs.oracle.com/javase/7/docs/api/java/util/TreeSet.html
- https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html
- https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html#sorted–
- 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/Map.Entry.html#comparingByValue-java.util.Comparator-
Happy Coding !!
Happy Learning !!