How to Check if LinkedHashMap Contains a value in Java?
Last Updated :
16 Nov, 2022
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");
}
}
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");
}
}
Time Complexity: O(1).
Similar Reads
How to Check if LinkedHashMap is Empty in Java? The LinkedHashMap is just like HashMap with an additional feature of maintaining an order of elements inserted into it. HashMap provided the advantage of quick insertion, search, and deletion but it never maintained the track and order of insertion which the LinkedHashMap provides where the elements
2 min read
How to Get a Value From LinkedHashMap by Index in Java? LinkedHashMap is a predefined class in Java which is similar to HashMap, contain key and its respective value unlike HashMap, In LinkedHashMap insertion order is preserved. The task is to get value from LinkedHashMap by their Index in other words, an order of their insertion. As an advantage of Link
4 min read
How to Convert all LinkedHashMap Values to a List in Java? The task is to convert all LinkedHashMap values to a list in java. LinkedHashMap is an implementation of a Map. The Map and List are two different data structures. The Map stores key-value pairs while the List is an ordered collection of elements. To convert all values of the LinkedHashMap to a List
2 min read
How to Get All the Values of the LinkedHashMap in Java? LinkedHashMap is a predefined class in Java that is similar to HashMap, contains key and its respective value, unlike HashMap. In LinkedHashMap insertion order is preserved. The task is to get all the values present in our LinkedHashMap that is linked with their respective key. Use Iteration or pred
4 min read
How to Update the Value of an Existing Key in a LinkedHashMap in Java? In Java programming, a LinkedHahMap is like HashMap with additional features in the Java Collections framework. It keeps track of the order in which elements were added. A regular HashMap doesn't have a fixed order for elements. LinkedHashMap uses an approach with a doubly-linked list to remember th
3 min read
How to iterate LinkedHashMap in Java? LinkedHashMap class extends HashMap and maintains a linked list of the entries in the map, in the order in which they were inserted. This allows insertion-order iteration over the map. That is, when iterating a LinkedHashMap, the elements will be returned in the order in which they were inserted. Th
2 min read
Checking if Element Exists in LinkedHashSet in Java The Java.util.LinkedHashSet.contains() method is used to check whether a specific element is present in the LinkedHashSet or not. So basically it is used to check if a Set contains any particular element. The contains a method of the LinkedHashSet class returns true if the specified element is found
1 min read
Sort LinkedHashMap by Values using Comparable Interface in Java The LinkedHashMap is just like HashMap with an additional feature of maintaining an order of elements inserted into it. Assuming you have gone through LinkedHashMap in java and know about LinkedHashMap. Syntax: int compare(T obj) ; Illustration: Input : { GEEKS=1, geeks=3, for=2 } Output : { GEEKS=1
2 min read
How to Find the Element Index in LinkedHashSet in Java? LinkedHashSet is used to store distinct items and get the items in which order they were inserted in Java. LinkedHashSet does not store values based on the index. But there are some methods to find the element index in LinkedHashSet in Java. Method 1: (By converting LinkedHashSet to ArrayList) To fi
4 min read