In this article we will discuss how to search into a Map by value and fetch associated keys to the given value.

Suppose we have a map of string and integers i.e.

HashMap<String, Integer> mapOfWords = new HashMap<String, Integer>() {
	{
		put("John", 25);
		put("at", 2);
		put("from", 10);
		put("is", 5);
		put("the", 2);
		put("hello", 1);
		put("break", 25);
	}
};

Now we want to get all the keys whose value is 2 i.e.  the &  at.  Let’s see How to do that,

Get Keys by Value in Map

To fetch the keys associated by given value, follow these steps,

  • First Check if given value exists in map using containsValue() function. If yes then,
  • Iterate over a given Map and for each Entry check if value matches the given value, if yes then store its key in the list.

Generic solution that will work with every map i.e. HashMap, TreeMap etc.

static <K, V> List<K> getAllKeysForValue(Map<K, V> mapOfWords, V value) 
{
	List<K> listOfKeys = null;
	 
	//Check if Map contains the given value
	if(mapOfWords.containsValue(value))
	{
		// Create an Empty List
		listOfKeys = new ArrayList<>();
				
		// Iterate over each entry of map using entrySet
		for (Map.Entry<K, V> entry : mapOfWords.entrySet()) 
		{
			// Check if value matches with given value
			if (entry.getValue().equals(value))
			{
				// Store the key from entry to the list
				listOfKeys.add(entry.getKey());
			}
		}
	}
	// Return the list of keys whose value matches with given value.
	return listOfKeys;	
}

Complete example is as follows,
package com.thispointer.java.hashmap.examples;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class MapUtilities {

	/*
	 * Get the all the keys associated with given Value V from map 
	 */
	static <K, V> List<K> getAllKeysForValue(Map<K, V> mapOfWords, V value) 
	{
		List<K> listOfKeys = null;
		 
		//Check if Map contains the given value
		if(mapOfWords.containsValue(value))
		{
			// Create an Empty List
			listOfKeys = new ArrayList<>();
					
			// Iterate over each entry of map using entrySet
			for (Map.Entry<K, V> entry : mapOfWords.entrySet()) 
			{
				// Check if value matches with given value
				if (entry.getValue().equals(value))
				{
					// Store the key from entry to the list
					listOfKeys.add(entry.getKey());
				}
			}
		}
		// Return the list of keys whose value matches with given value.
		return listOfKeys;	
	}


	public static void main(String[] args) {
		
		// Create a Map of words and thier frequency count
		HashMap<String, Integer> mapOfWords = new HashMap<String, Integer>() {
			{
				put("John", 25);
				put("at", 2);
				put("from", 10);
				put("is", 5);
				put("the", 2);
				put("hello", 1);
				put("break", 25);
			}
		};

		//Get the list of keys whose value matches with given value.
		List<String> listOfKeys = MapUtilities.getAllKeysForValue(mapOfWords, 2);

		System.out.println(listOfKeys);


	}
}

Output:
[the, at]

 

 

Join a list of 2000+ Programmers for latest Tips & Tutorials