In this article, we will discuss how to find an entry with Largest Key in a HashMap using Java 8 Stream
Find an entry with Largest Key in a HashMap :
To find an entry with largest Key,
- First sort the HashMap in descending–order of Keys
- Then get the 1st entry from the reverse–sorted Map
There are different ways to sort the Map/HashMap according to Keys, read below articles before proceeding further
- Java 8 – How to Sort a Map entries by its Key in 6 ways ?
- Java 8 – Find First and Last entries in a Map or HashMap ?
Here, we will discuss only 2 approaches to find an entry with Largest Key from HashMap
- Using Java 8 Stream
- Before Java 8
1. Using Java 8 Stream :
- First step is to sort the Map entries in descending–order of Keys which will give an entry with Largest Key at the top of the Map
- To sort Map entries in descending-order of Keys, use Stream.sorted() method passing the argument Map.Entry.comparingByKey(Comparator.reverseOrder())
- Get the 1st entry from sorted Map using Stream.findFirst() method
FindEntryWithLargestKeyInMapUsingJava8Stream.java
package in.bench.resources.find.entry.map;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
public class FindEntryWithLargestKeyInMapUsingJava8Stream {
public static void main(String[] args) {
// 1. creating HashMap object of type <Integer, String>
Map<Integer, String> populationCountry = new HashMap<>();
// 1.1 adding key-value pairs to HashMap object
populationCountry.put(220892331, "Pakistan");
populationCountry.put(146748590, "Russia");
populationCountry.put(213728559, "Brazil");
populationCountry.put(382357386, "Indian");
populationCountry.put(332429717, "America");
// 1.2 print original Map entries
System.out.println("1. Original Map Entries :- \n");
// 1.3 print Map entries to console
populationCountry.forEach((key, value) -> System.out.println(
"Key : " + key + "\t\t" + "Value : " + value
));
// 2. Sort Keys using Java 8 Stream.sorted() method & get 1st Entry
Entry<Integer, String> entryWithLargestKey = populationCountry
.entrySet()
.stream()
.sorted(Map.Entry.comparingByKey(Comparator.reverseOrder()))
.findFirst()
.get();
// 2.2 print Map.Entry with Largest Key
System.out.println("\n\n2. Map Entry with Largest Key :- " + entryWithLargestKey);
}
}
Output :
1. Original Map Entries :-
Key : 382357386 Value : Indian
Key : 220892331 Value : Pakistan
Key : 146748590 Value : Russia
Key : 213728559 Value : Brazil
Key : 332429717 Value : America
2. Map Entry with Largest Key :- 382357386=Indian
2. Before Java 8 :
- First step is to sort the Map entries in descending–order of Keys which will give an entry with Largest Key at the top of the Map
- To sort Map entries in descending–order of Keys, convert HashMap to TreeMap passing Collections.reverseOrder() method as constructor–argument to TreeMap object
- Get the 1st entry from sorted Map by iterating using enhanced for–loop and then break the loop
FindEntryWithLargestKeyInMap.java
package in.bench.resources.find.entry.map;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
import java.util.Map.Entry;
public class FindEntryWithLargestKeyInMap {
public static void main(String[] args) {
// 1. creating HashMap object of type <Integer, String>
Map<Integer, String> populationCountry = new HashMap<>();
// 1.1 adding key-value pairs to HashMap object
populationCountry.put(220892331, "Pakistan");
populationCountry.put(146748590, "Russia");
populationCountry.put(213728559, "Brazil");
populationCountry.put(382357386, "Indian");
populationCountry.put(332429717, "America");
// 1.2 print original Map entries
System.out.println("1. Original Map Entries :- \n");
// 1.3 print Map entries to console
populationCountry.forEach((key, value) -> System.out.println(
"Key : " + key + "\t\t" + "Value : " + value
));
// 2. convert HashMap to TreeMap in descending-order of Keys
Map<Integer, String> treeMapInDescKeys = new TreeMap<>(Collections.reverseOrder());
// 2.1 add unsorted map to TreeMap for reverse-order sorting of Keys
treeMapInDescKeys.putAll(populationCountry);
// 2.2 local variable to assign Entry with Largest Key
Entry<Integer, String> entryWithLargestKey = null;
// 2.3 iterate and get 1st entry from reverse-sorted Map
for(Entry<Integer, String> entry : treeMapInDescKeys.entrySet()) {
// first entry will be largest, as it sorted in descending-order of keys
if(null == entryWithLargestKey) {
entryWithLargestKey = entry;
break;
}
}
// 2.4 print Map.Entry with Largest Key
System.out.println("\n\n2. Map Entry with Largest Key :- " + entryWithLargestKey);
}
}
Output :
1. Original Map Entries :-
Key : 382357386 Value : Indian
Key : 220892331 Value : Pakistan
Key : 146748590 Value : Russia
Key : 213728559 Value : Brazil
Key : 332429717 Value : America
2. Map Entry with Largest Key :- 382357386=Indian
Related Articles :
- Java 8 – How to find an entry with Largest Key in a Map or HashMap ?
- Java 8 – How to find an entry with Largest Value in a Map or HashMap ?
- Java 8 – How to find an entry with Smallest Key in a Map or HashMap ?
- Java 8 – How to find an entry with Smallest Value in a Map or HashMap ?
- Java 8 – How to find an entry based on the Key in a Map or HashMap ?
- Java 8 – How to find an entry based on the Value in a Map or HashMap ?
- Java 8 – How to remove an entry from HashMap by comparing keys ?
- Java 8 – How to remove an entry from HashMap by comparing values ?
- Java 8 – How to remove an entry with Largest Key in a Map or HashMap ?
- Java 8 – How to remove an entry with Largest Value in a Map or HashMap ?
- Java 8 – How to remove an entry with Smallest Key in a Map or HashMap ?
- Java 8 – How to remove an entry with Smallest Value in a Map or HashMap ?
- Java 8 – How to remove an entry based on the Key in a Map or HashMap ?
- Java 8 – How to remove an entry based on the Value in a Map or HashMap ?
References :
- 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/TreeMap.html
- https://docs.oracle.com/javase/8/docs/api/java/util/Map.Entry.html
- https://docs.oracle.com/javase/8/docs/api/java/util/Comparator.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Collections.html
Happy Coding !!
Happy Learning !!