In this article, we will discuss how to remove an entry or entries from HashMap using Java 8 APIs
Before proceeding further, it is recommended to understand few terms w.r.t Map
1. Remove entry/entries using removeIf() method by comparing values :
- removeIf(); –> Removes all of the elements of this collection that satisfy the given predicate
- Syntax: default boolean removeIf(Predicate filter);
- We will cover different cases to remove entry/entries using removeIf() method of Java 8
1.1 Removing an entry using Equal-To operator (==)
RemovingEntryFromHashMap.java
package in.bench.resources.map.java8.example;
import java.util.HashMap;
import java.util.Map;
public class RemovingEntryFromHashMap {
public static void main(String[] args) {
// create HashMap
Map<String, Integer> map = new HashMap<String, Integer>();
// put few items
map.put("Sachin", 200);
map.put("Afrifi", 27);
map.put("Pietersen", 104);
map.put("Lara", 131);
map.put("Ponting", 168);
// printing map before removeIf
System.out.println("Before removing entry from Map : \n"
+ map);
// removing an Entry using Java 1.8 (boolean equalTo expression)
map.entrySet().removeIf(
matches -> matches.getValue()
.compareTo(Integer.valueOf(27)) == 0);
// printing map after removeIf
System.out.println("\n\nAfter removing entry from Map : \n"
+ map);
}
}
Output:
Before removing entry from Map :
{Lara=131, Afrifi=27, Pietersen=104, Ponting=168, Sachin=200}
After removing entry from Map :
{Lara=131, Pietersen=104, Ponting=168, Sachin=200}
1.2 Removing all entries using Less-Than operator (<)
RemovingAllEntriesLessThan.java
package in.bench.resources.map.java8.example;
import java.util.HashMap;
import java.util.Map;
public class RemovingAllEntriesLessThan {
public static void main(String[] args) {
// create HashMap
Map<String, Integer> map = new HashMap<String, Integer>();
// put few items
map.put("Sachin", 200);
map.put("Afrifi", 27);
map.put("Pietersen", 104);
map.put("Lara", 131);
map.put("Ponting", 168);
// printing map before removeIf
System.out.println("Before removing entry from Map : \n"
+ map);
// removing all entries less-than 120 matches
map.entrySet().removeIf(
matches -> matches.getValue()
.compareTo(Integer.valueOf(120)) < 0);
// printing map after removeIf
System.out.println("\n\nAfter removing entry from Map : \n"
+ map);
}
}
Output:
Before removing entry from Map :
{Lara=131, Afrifi=27, Pietersen=104, Ponting=168, Sachin=200}
After removing entry from Map :
{Lara=131, Ponting=168, Sachin=200}
1.3 Removing all entries using Greater-Than operator (>)
RemovingAllEntriesGreaterThan.java
package in.bench.resources.map.java8.example;
import java.util.HashMap;
import java.util.Map;
public class RemovingAllEntriesGreaterThan {
public static void main(String[] args) {
// create HashMap
Map<String, Integer> map = new HashMap<String, Integer>();
// put few items
map.put("Sachin", 200);
map.put("Afrifi", 27);
map.put("Pietersen", 104);
map.put("Lara", 131);
map.put("Ponting", 168);
// printing map before removeIf
System.out.println("Before removing entry from Map : \n"
+ map);
// removing all entries greater-than 150 matches
map.entrySet().removeIf(
matches -> matches.getValue()
.compareTo(Integer.valueOf(150)) > 0);
// printing map after removeIf
System.out.println("\n\nAfter removing entry from Map : \n"
+ map);
}
}
Output:
Before removing entry from Map :
{Lara=131, Afrifi=27, Pietersen=104, Ponting=168, Sachin=200}
After removing entry from Map :
{Lara=131, Afrifi=27, Pietersen=104}
In the next article, we will discuss how to remove entry/entries from HashMap by comparing Key using Java 8 APIs
Hope, you found this article very helpful. If you have any suggestion or want to contribute any other way or tricky situation you faced during Interview hours, then share with us. We will include that code here
Happy Coding !!
Happy Learning !!