In this article, we will discuss how to remove an entry based on the Value in a HashMap using Java 8 Stream
Remove an entry based on Value in a HashMap :
To remove an entry based on the Value,
- First iterate through entrySet
- Remove the entry by comparing the Value passed
Here, we will discuss 2 approaches to remove an entry based on the Value from HashMap
- Using Java 8 Stream
- Before Java 8
1. Using Java 8 Stream :
- Check entry set and remove required entry by comparing Value using removeIf() method
- Now there are 2 possibilities on comparison,
- If the entry is present comparing the supplied Value, then corresponding entry will be removed from the original Map entries
- If it is not present, then original Map won’t be affected and all entries will be present
- Note: If there are duplicate values for the different unique Keys, then it will remove all entries corresponding to that Value
RemoveEntryBasedOnValueInMapUsingJava8Stream.java
package in.bench.resources.find.entry.map;
import java.util.HashMap;
import java.util.Map;
public class RemoveEntryBasedOnValueInMapUsingJava8Stream {
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. find Entry on the basis of Value
String valueToFind = "Pakistan";
// 2.1 Remove Entry based on Value
populationCountry
.entrySet()
.removeIf(entry -> entry.getValue().equals(valueToFind));
// 2.2 print to console
System.out.println("\n\n2. After removing Map entry with Value '" + valueToFind + "' :- \n");
// 2.3 Map entries after removing a particular Value
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. After removing Map entry with Value 'Pakistan' :-
Key : 382357386 Value : Indian
Key : 146748590 Value : Russia
Key : 213728559 Value : Brazil
Key : 332429717 Value : America
2. Before Java 8 :
- Iterate through Map values one-by-one and remove required entry by comparing Value using remove() method
- Now there are 2 possibilities on comparison,
- If the value is present, then corresponding entry will be removed from the original Map entries
- If it is not present, then original Map won’t be affected and all entries will be present
- Note: If there are duplicate values for the different unique Keys, then it will remove all entries corresponding to that Value
RemoveEntryBasedOnValueInMap.java
package in.bench.resources.find.entry.map;
import java.util.HashMap;
import java.util.Map;
public class RemoveEntryBasedOnValueInMap {
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. find Entry on the basis of Value
String valueToFind = "Pakistan";
// 2.1 Remove Entry based on Value
populationCountry
.values()
.remove(valueToFind);
// 2.2 print to console
System.out.println("\n\n2. After removing Map entry with Value '" + valueToFind + "' :- \n");
// 2.3 Map entries after removing a particular Value
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. After removing Map entry with Value 'Pakistan' :-
Key : 382357386 Value : Indian
Key : 146748590 Value : Russia
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 !!