Skip to content
Related Articles

Related Articles

Check if Particular Value Exists in Java HashMap
  • Last Updated : 28 Dec, 2020
GeeksforGeeks - Summer Carnival Banner

Java HashMap is an implementation of the Map interface which maps a Value to a Key which essentially forms an associative pair wherein we can call a Value based on the Key. Java HashMap provides a lot of advantages such as allowing different data types for the Key and Value which makes this data structure more inclusive and versatile. It also allows null for the Key and the Value given that one of the two forming a pair is not null.

The different approaches to check for the existence of a particular Value in a Java HashMap are:

  1. Using the built in containsValue() method of the HashMap class
  2. Creating a map from the entries of the HashMap and then iterating through the Values
  3. Creating an ArrayList from the Values of the HashMap and then iterating through this list

Approach 1 :

This approach provides a fairly simple and efficient method to check for the existence of a Value in a HashMap using the containsValue() predefined method which returns a boolean value.

Syntax:



Hash_Map.containsValue(Object Value)

Parameters: The method takes just one parameter Value of Object type and refers to the value whose mapping is supposed to be checked by any key inside the map.

Return Value: The method returns boolean true if the mapping of the value is detected else false.

Algorithm :

  1. Declare a method with return type boolean to check for the existence of the value.
  2. Initialize a HashMap describing the data types for the Key as well as the Values.
  3. Fill this HashMap with Key-Value pairs using the built in put() method of the HashMap class.
  4. Declare a boolean variable to store the result of the containsValue() method.

Code :

Java




// java program to check if a particular
// value exists in a HashMap
  
import java.util.*;
  
class GFG {
  
    // declaring the method to return
    // if the value is present or not
    // the parameter valueToBeChecked
    // represents the value to be checked
    boolean checkForValue(int valueToBeChecked)
    {
  
        // initializing the HashMap
        HashMap<String, Integer> hashMap = new HashMap<>();
  
        // filling the HashMap with
        // key value pairs
        hashMap.put("key1", 1);
        hashMap.put("key2", 2);
        hashMap.put("key3", 3);
        hashMap.put("key4", 4);
  
        // declaring the variable to store
        // the result
        // calling the containsValue() method
        boolean result
            = hashMap.containsValue(valueToBeChecked);
  
        // returning the result
        return result;
    }
  
    // Driver Code
    public static void main(String[] args)
    {
  
        // instantiating the class
        GFG ob = new GFG();
  
        // displaying and calling the
        // checkForValue() method
        System.out.println(ob.checkForValue(10));
    }
}
Output
false

Approach 2 :

Using this approach, we create a Map from all the entries of the HashMap, Keys and Values included and then iterate only through the Values to check for the specific one.

Algorithm :



  1. Repeat steps 1 through 3 as described in the first approach to create a HashMap.
  2. Then create a Map using a for each loop and the predefined entrySet() method of the HashMap.
  3. Iterate through the values in the Map using the predefined getValue() method of the Map class.
  4. At each iteration, compare the Value with the specified Value and return the corresponding result.

Code :

Java




// java program to check if a particular
// value exists in HashMap
  
import java.util.*;
  
class GFG {
  
    // declaring the method
    // the parameter is the variable which
    // stores the value to be checked
    boolean checkForValue(int valueToBeChecked)
    {
  
        // initializing the HashMap
        HashMap<String, Integer> hashMap = new HashMap<>();
  
        // filling up the HashMap with
        // Key-Value pairs
        hashMap.put("key1", 1);
        hashMap.put("key2", 2);
        hashMap.put("key3", 3);
        hashMap.put("key4", 4);
  
        // for each loop
        // all the entries in the HashMap are
        // stored in the Map
        for (Map.Entry<String, Integer> mapEntries :
             hashMap.entrySet()) {
  
            // fetching the values of the HashMap
            // one at a time and comparing with
            // the value to be checked
            if (mapEntries.getValue() == valueToBeChecked)
                return true;
        }
        return false;
    }
  
    // Driver Code
    public static void main(String[] args)
    {
  
        // instantiating the class
        GFG ob = new GFG();
  
        // displaying and calling the
        // checkForValue() method
        System.out.println(ob.checkForValue(2));
    }
}
Output
true

Approach 3 :

Under the third approach, we create an ArrayList from the values of the HashMap and iterate through the entire list to check for the specific value mentioned.

ALlgorithm :

  1. Repeat steps 1 through 3 to create a HashMap.
  2. Next, create an ArrayList of the same data type as the values in the HashMap.
  3. In the next step we declare an iterator to iterate through the entire ArrayList till it finds the specific value.
  4. Using a while loop iterate through the values stored in the ArrayList and in each iteration checking with the specified value.
  5. Return the corresponding result as a boolean value.

Code :

Java




// java program to check if a particular
// key exists in a HashMap
  
import java.util.*;
  
class GFG {
  
    // declaring the method
    // the parameter spcifies the value to
    // be checked
    boolean checkForValue(int valueToBeChecked)
    {
  
        // initializing the HashMap
        HashMap<String, Integer> hashMap = new HashMap<>();
  
        // filling up the HashMap with
        // Key-Value pairs
        hashMap.put("key1", 1);
        hashMap.put("key2", 2);
        hashMap.put("key3", 3);
        hashMap.put("key4", 4);
  
        // declaring an ArrayList of type
        // Integer from the values of the
        // HashMap using the predefined
        // values() method of the HashMap class
        ArrayList<Integer> listOfValues
            = new ArrayList<>(hashMap.values());
  
        // declaring the iterator
        Iterator<Integer> itr = listOfValues.iterator();
  
        // iterating through the list
        while (itr.hasNext()) {
  
            // comparing each value with the
            // one specified
            if (itr.next() == valueToBeChecked)
                return true;
        }
        return false;
    }
  
    // Driver Code
    public static void main(String[] args)
    {
  
        // instantiating the class
        GFG ob = new GFG();
  
        // displaying and calling the
        // checkForValue() method
        System.out.println(ob.checkForValue(0));
    }
}
Output
false

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :