In this article, we will discuss how to find an entry based on the Key in a HashMap using Java 8 Stream
Find an entry based on Key in a HashMap :
To find an entry based on the Key,
- First iterate through entrySet
- Then get the entry by comparing the Key
Here, we will discuss 2 approaches to find an entry based on the Key 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 Key using Stream.filter() method
- Now there are 2 possibilities on comparison,
- If the entry is present with the supplied Key, then corresponding entry will be printed to the console
- If it is not present, nothing will be printed in the console
FindEntryBasedOnKeyInMapUsingJava8Stream.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 FindEntryBasedOnKeyInMapUsingJava8Stream { public static void main(String[] args) { // 1. creating HashMap object of type <String, Integer> Map<String, Integer> countryPopulation = new HashMap<>(); // 1.1 adding key-value pairs to HashMap object countryPopulation.put( "Pakistan" , 220892331 ); countryPopulation.put( "Russia" , 146748590 ); countryPopulation.put( "Brazil" , 213728559 ); countryPopulation.put( "Indian" , 382357386 ); countryPopulation.put( "America" , 332429717 ); // 1.2 print original Map entries System.out.println( "1. Original Map Entries :- \n" ); // 1.3 print Map entries to console countryPopulation.forEach((key, value) -> System.out.println( "Key : " + key + "\t\t" + "Value : " + value )); // 2. find Entry on the basis of Key String keyToFind = "Brazil" ; // 2.1 print to console System.out.println( "\n\n2. Entry which has Key '" + keyToFind + "'\n" ); // 2.2 Iterate and find Entry based on Key countryPopulation .entrySet() .stream() .filter(entry -> entry.getKey().equals(keyToFind)) .forEach(System.out::println);; } } |
Output :
1 2 3 4 5 6 7 8 9 10 11 12 | 1. Original Map Entries :- Key : Pakistan Value : 220892331 Key : America Value : 332429717 Key : Brazil Value : 213728559 Key : Indian Value : 382357386 Key : Russia Value : 146748590 2. Entry which has Key 'Brazil' Brazil=213728559 |
2. Before Java 8 :
- Iterate through entry set using enhanced for-loop and filter required entry by comparing Key using if–condition
- Now there are 2 possibilities on comparison,
- If the entry is present with the supplied Key, then corresponding entry will be printed to the console
- If it is not present, nothing will be printed in the console
FindEntryBasedOnKeyInMap.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 FindEntryBasedOnKeyInMap { public static void main(String[] args) { // 1. creating HashMap object of type <String, Integer> Map<String, Integer> countryPopulation = new HashMap<>(); // 1.1 adding key-value pairs to HashMap object countryPopulation.put( "Pakistan" , 220892331 ); countryPopulation.put( "Russia" , 146748590 ); countryPopulation.put( "Brazil" , 213728559 ); countryPopulation.put( "Indian" , 382357386 ); countryPopulation.put( "America" , 332429717 ); // 1.2 print original Map entries System.out.println( "1. Original Map Entries :- \n" ); // 1.3 print Map entries to console countryPopulation.forEach((key, value) -> System.out.println( "Key : " + key + "\t\t" + "Value : " + value )); // 2. find Entry on the basis of Key String keyToFind = "Brazil" ; // 2.1 print to console System.out.println( "\n\n2. Entry which has Key '" + keyToFind + "'\n" ); // 2.2 Iterate and find Entry based on Key for (Map.Entry<String, Integer> entry : countryPopulation.entrySet()) { if (entry.getKey().equals(keyToFind)) { System.out.println(entry); } } } } |
Output :
1 2 3 4 5 6 7 8 9 10 11 12 | 1. Original Map Entries :- Key : Pakistan Value : 220892331 Key : America Value : 332429717 Key : Brazil Value : 213728559 Key : Indian Value : 382357386 Key : Russia Value : 146748590 2. Entry which has Key 'Brazil' Brazil=213728559 |
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 !!