How to Check if LinkedHashMap Contains a value in Java? - GeeksforGeeks

15 Dec, 2020
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");    }}
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.
Psuedo 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");    }}
False
Time Complexity: O(1).
Java-LinkedHashMap
Picked
Technical Scripter 2020
Java
Java Programs
Technical Scripter
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Stream In Java
Constructors in Java
Exceptions in Java
Functional Interfaces in Java
Different ways of Reading a text file in Java
Java Programming Examples
Convert Double to Integer in Java
Implementing a Linked List in Java using Class
How to Iterate HashMap in Java?
Program to print ASCII Value of a character