In this article, we will discuss how to remove an entry with Smallest Key in a HashMap using Java 8 Stream
Remove an entry with Smallest Key in a HashMap :
To remove an entry with Smallest Key,
- First sort the HashMap in ascending–order of Keys
- Then get the 1st entry from the natural–sorted Map
- Finally, remove the 1st entry by comparing the Key
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 ?
- Java 8 – How to remove an entry from HashMap by comparing keys ?
Here, we will discuss only 2 approaches to remove an entry with Smallest Key from HashMap
- Using Java 8 Stream
- Before Java 8
1. Using Java 8 Stream :
- First step is to sort the Map entries in ascending–order of Keys which will give an entry with Smallest Key at the top of the Map
- To sort Map entries in ascending-order of Keys, use Stream.sorted() method passing the argument Map.Entry.comparingByKey(Comparator.naturalOrder())
- Get the 1st entry from sorted Map using Stream.findFirst() method
- Finally, remove the 1st entry by comparing the Smallest Key obtained from the previous step using removeIf() method
RemoveEntryWithSmallestKeyInMapUsingJava8Stream.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 RemoveEntryWithSmallestKeyInMapUsingJava8Stream {
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> entryWithSmallestKey = populationCountry
.entrySet()
.stream()
.sorted(Map.Entry.comparingByKey(Comparator.naturalOrder()))
.findFirst()
.get();
// 2.2 print Map.Entry with Smallest Key
System.out.println("\n\n2. Map Entry with Smallest Key :- " + entryWithSmallestKey);
// 3. remove entry with smallest key
populationCountry
.entrySet()
.removeIf(entry -> entry.getKey().equals(entryWithSmallestKey.getKey()));
// 3.1 print to console
System.out.println("\n\n3. Map entries after removing entry with Smallest Key :- \n");
// 3.2 print Map entries after removing entry with Smallest Key
populationCountry.forEach((key, value) -> System.out.println(
"Key : " + key + "\t\t" + "Value : " + value
));
}
}
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 Smallest Key :- 146748590=Russia
3. Map entries after removing entry with Smallest Key :-
Key : 382357386 Value : Indian
Key : 220892331 Value : Pakistan
Key : 213728559 Value : Brazil
Key : 332429717 Value : America
2. Before Java 8 :
- First step is to sort the Map entries in ascending–order of Keys which will give an entry with Smallest Key at the top of the Map
- To sort Map entries in ascending–order of Keys, convert HashMap to TreeMap which stores Map entries in natural–order of Keys
- Get the 1st entry from sorted Map by iterating using enhanced for–loop and then break the loop
- Finally, remove the 1st entry by comparing the Smallest Key obtained from the previous step using remove() method
RemoveEntryWithSmallestKeyInMap.java
package in.bench.resources.find.entry.map;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
public class RemoveEntryWithSmallestKeyInMap {
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 ascending-order of Keys
Map<Integer, String> treeMapInAscKeys = new TreeMap<>();
// 2.1 add unsorted map to TreeMap for natural-order sorting of Keys
treeMapInAscKeys.putAll(populationCountry);
// 2.2 local variable to assign Entry with Smallest Key
Entry<Integer, String> entryWithSmallestKey = null;
// 2.3 iterate and get 1st entry from natural-sorted Map
for(Entry<Integer, String> entry : treeMapInAscKeys.entrySet()) {
// first entry will be smallest, as it sorted in ascending-order of keys
if(null == entryWithSmallestKey) {
entryWithSmallestKey = entry;
break;
}
}
// 2.4 print Map.Entry with Smallest Key
System.out.println("\n\n2. Map Entry with Smallest Key :- " + entryWithSmallestKey);
// 3. remove entry with smallest key
populationCountry
.keySet()
.remove(entryWithSmallestKey.getKey());
// 3.1 print to console
System.out.println("\n\n3. Map entries after removing entry with Smallest Key :- \n");
// 3.1 print Map entries after removing entry with Smallest Key
populationCountry.forEach((key, value) -> System.out.println(
"Key : " + key + "\t\t" + "Value : " + value
));
}
}
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 Smallest Key :- 146748590=Russia
3. Map entries after removing entry with Smallest Key :-
Key : 382357386 Value : Indian
Key : 220892331 Value : Pakistan
Key : 213728559 Value : Brazil
Key : 332429717 Value : America
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 !!