In this article, we will discuss how to find an entry based on the Value in a HashMap using Java 8 Stream
Find an entry based on Value in a HashMap :
To find an entry based on the Value,
- First iterate through entrySet
- Then get the entry by comparing the Value
Here, we will discuss 2 approaches to find an entry based on the Value from HashMap
- Using Java 8 Stream
- Before Java 8
1. Using Java 8 Stream :
- Iterate through entry set using Stream and filter required entry by comparing Value using Stream.filter() method
- Now there are 2 possibilities on comparison,
- If the entry is present with the supplied Value, then corresponding entry will be printed to the console
- If it is not present, nothing will be printed in the console
- Note: If there are duplicate values for the different unique Keys, then it will print all entries corresponding to that Value
FindEntryBasedOnValueInMapUsingJava8Stream.java
package in.bench.resources.find.entry.map;
import java.util.HashMap;
import java.util.Map;
public class FindEntryBasedOnValueInMapUsingJava8Stream {
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 print to console
System.out.println("\n\n2. Entry which has Value '" + valueToFind + "'\n");
// 2.2 Iterate and find Entry based on Value
populationCountry
.entrySet()
.stream()
.filter(entry -> entry.getValue().equals(valueToFind))
.forEach(System.out::println);
}
}
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. Entry which has Value 'Pakistan'
220892331=Pakistan
2. Before Java 8 :
- Iterate through entry set using enhanced for-loop and filter required entry by comparing Value using if–condition
- Now there are 2 possibilities on comparison,
- If the entry is present with the supplied Value, then corresponding entry will be printed to the console
- If it is not present, nothing will be printed in the console
- Note: If there are duplicate values for the different unique Keys, then it will print all entries corresponding to that Value
FindEntryBasedOnValueInMap.java
package in.bench.resources.find.entry.map;
import java.util.HashMap;
import java.util.Map;
public class FindEntryBasedOnValueInMap {
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 print to console
System.out.println("\n\n2. Entry which has Value '" + valueToFind + "'\n");
// 2.2 Iterate and find Entry based on Value
for(Map.Entry<Integer, String> entry : populationCountry.entrySet()) {
if(entry.getValue().equals(valueToFind)) {
System.out.println(entry);
}
}
}
}
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. Entry which has Value 'Pakistan'
220892331=Pakistan
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 !!