Java 8 – How to find an entry based on the Value in a Map or HashMap ?

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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
2
3
4
5
6
7
8
9
10
11
12
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 ifcondition
  • 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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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
2
3
4
5
6
7
8
9
10
11
12
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 :

References :

Happy Coding !!
Happy Learning !!

Java 8 - How to remove an entry based on the Value in a Map or HashMap ?
Java 8 - How to remove an entry based on the Key in a Map or HashMap ?