Open In App

How to Check if LinkedHashMap Contains a value in Java?

Last Updated : 16 Nov, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

LinkedHashMap is a predefined class in Java which is similar to HashMap, contains key and its respective value unlike HashMap, In LinkedHashMap insertion order is preserved. The task is to check if  LinkedHashMap contains any value in java. to check we have to iterate through our LinkedHashMap and if we get any value we return true.

Example :

Input : Key- 2 : Value-6
    Key- 4 : Value-1
    Key- 5 : Value-10
    value to check - 2

Output : False

Input : Key- 1 : Value-15
    Key- 3 : Value-12
    Key- 5 : Value-9
    Key- 6 : Value-11
    Value to check - 11

Output : True

Approach 1:(Using entrySet() Method)

Use a For-each loop to iterate through LinkedHashMap. create check() function to if there exist any value in LinkedHasMap or nor. Iterate through LinkedHashMap if we get any value return true else return false.

Pseudo Code:

for (Map.Entry<Integer, Integer> it : lhm.entrySet()) {
       if (it.getValue() != null)
                return true;
}

Syntax:

linked_hash_map.entrySet()

Parameters: The method does not take any parameter.

Return Value: The method returns a set having the same elements as the LinkedHashMap.

Example:

Java




// Java program to check if LinkedHashMap contains a
// particular value
 
import java.util.*;
import java.io.*;
 
class GFG {
 
    public static boolean
    check(LinkedHashMap<Integer, Integer> lhm, int value)
    {
        // iterate the map and find
        for (Map.Entry<Integer, Integer> it :
             lhm.entrySet()) {
            if (it.getValue() == value)
                return true;
        }
 
        return false;
    }
    public static void main(String[] args)
    {
        // create a linked Hashmap
        LinkedHashMap<Integer, Integer> LHM
            = new LinkedHashMap<>();
 
        // add elements
        LHM.put(2, 6);
        LHM.put(4, 1);
        LHM.put(5, 10);
        int value = 2;
 
        // check if has a value 2
        if (check(LHM, value))
            System.out.println("True");
        else
            System.out.println("False");
    }
}


Output

False

Time complexity: O(n).

Approach 2: (Using containsValue() Method)

This approach is an optimal approach to our problem. In the above approach, we iterate through our LinkedHashMap. In this approach, we directly use our pre-defined function to check our value.

Algorithm

Use  the function containValue() to find if our value is present or not in our LinkedHashMap.

Pseudo Code:

LHM.containsValue(val)

Here, 

Val is value to check.

LHM is name of our LinkedHashMap.

Syntax:

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

Example:

Java




// Java program to check if LinkedHashMap contains a
// particular value
 
import java.util.*;
import java.io.*;
 
class GFG {
    public static void main(String[] args)
    {
          // create a linked hashmap
        LinkedHashMap<Integer, Integer> LHM
            = new LinkedHashMap<>();
 
          // add mappings
        LHM.put(2, 6);
        LHM.put(4, 1);
        LHM.put(5, 10);
        int value = 2;
 
          // check if it has a value
        if (LHM.containsValue(value))
            System.out.println("True");
        else
            System.out.println("False");
    }
}


Output

False

Time Complexity: O(1).



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads