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

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

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. 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 ifcondition
  • 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

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. 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 :

References :

Happy Coding !!
Happy Learning !!

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