Java Program to Get key from HashMap using the value



Java HashMap is a hash table-based implementation of Java's Map interface. It is a collection of key-value pairs. In this article, we will understand how to get a key from a HashMap using the value.

Following are the ways to get a key from a HashMap using the value:

Using For Loop

We can iterate through the entries of the HashMap using a for loop and check if the value matches the one we are looking for. If it does, we return the corresponding key using the getKey() method of the Map.Entry interface.

Steps to Follow

  • Create a HashMap of integer and string values and initialize elements in it using the ?put' method.
  • Define a key value.
  • Iterate over the elements of HashMap, and check if the key previously defined is present in the HashMap.
  • If found, break away from the loop.

Example

Following is an example of how to get key from a HashMap using the value with a for loop.

import java.util.HashMap;
import java.util.Map;
public class GetKeyFromValue {
   public static void main(String[] args) {
      Map<Integer, String> map = new HashMap<>();
      map.put(1, "One");
      map.put(2, "Two");
      map.put(3, "Three");
      map.put(4, "Four");

      String valueToFind = "Three";
      Integer keyFound = null;
      
      for(Map.Entry<Integer, String> entry : map.entrySet()) {
         if (entry.getValue().equals(valueToFind)) {
            keyFound = entry.getKey();
            break;
         }
      }

      if(keyFound != null) {
         System.out.println("Key for value '" + valueToFind + "' is: " + keyFound);
      } else {
         System.out.println("Value '" + valueToFind + "' not found in the map.");
      }
   }
}

Following is the output of the above code:

Key for value 'Three' is: 3

Using Stream API

We will use the Stream API to filter the entries of the hashmap according to the value we are looking for. The filter() method will be used to filter the entries, and the findFirst() method will return the first entry that matches the condition.

Steps to Follow

  • Create a HashMap of integer and string values and initialize elements in it using the put() method.
  • Define a key value.
  • Use the stream() method to create a stream of the entries of the HashMap.
  • Then filter() and the findFirst() method to find the first entry that matches the value.

Example

Following is an example of how to get key from a HashMap using the value with the Stream API.

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

public class GetKeyFromValue {
   public static void main(String[] args) {
      Map<Integer, String> map = new HashMap<>();
      map.put(1, "One");
      map.put(2, "Two");
      map.put(3, "Three");
      map.put(4, "Four");

      String valueToFind = "Three";

      Optional<Integer> keyFound = map.entrySet()
         .stream()
         .filter(entry -> entry.getValue().equals(valueToFind))
         .map(Map.Entry::getKey)
         .findFirst();

      if(keyFound.isPresent()) {
         System.out.println("Key for value '" + valueToFind + "' is: " + keyFound.get());
      } else {
         System.out.println("Value '" + valueToFind + "' not found in the map.");
      }
   }
}

Following is the output of the above code:

Key for value 'Three' is: 3
Aishwarya Naglot
Aishwarya Naglot

Writing clean code… when the bugs aren’t looking.

Updated on: 2025-06-18T16:36:37+05:30

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements