Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check if a particular value exists in Java LinkedHashMap
LinkedHashMap is a class in Java that implements the Map interface. It uses a doubly linked list to maintain the insertion order of elements. So, when we loop through a LinkedHashMap, the elements will be returned in the order we had inserted them.
The given task is to check if a particular value exists in a LinkedHashMap in Java. Let's look at some scenarios to understand the problem better:
Scenario
Input: {1=Mikasa, 2=Eren, 3=Reiner}, Value to check: Mikasa
Output: Value Exists
Explanation: The LinkedHashMap has the value that we are looking for.
Checking if a Value Exists in LinkedHashMap
The following are the ways we can check if a particular value exists in a LinkedHashMap in Java:
Using containsValue() Method
The containsValue() method is from the Map interface, which is implemented by the LinkedHashMap class. This method is used to check if a particular value exists in the LinkedHashMap. If the value exists, it returns TRUE; otherwise, it returns FALSE.
Syntax
Following is the syntax of the containsValue() method:
map.containsValue(value);
The following are the parameters:
- map is the object we create of the LinkedHashMap class
- value is the value we want to check.
Example
In the following example, we are searching for a particular value in a LinkedHashMap using the containsValue() method:
import java.util.LinkedHashMap;
public class CheckIfValueExists {
public static void main(String[] args){
LinkedHashMap<Integer, String> map = new LinkedHashMap<>();
map.put(1, "Kisaki");
map.put(2, "Takemichi");
map.put(3, "Mikey");
map.put(4, "Draken");
String valueToCheck = "Mikey";
if(map.containsValue(valueToCheck)) {
System.out.println("Value exists in the LinkedHashMap" + " and that is: " + valueToCheck);
} else {
System.out.println("Value does not exist in the LinkedHashMap");
}
}
}
When you run the above code, it will produce the following output:
Value exists in the LinkedHashMap and it is: Mikey
Using entrySet() Method
The entrySet() is another method from the Map Interface. This method returns a Set view (set of key-value pairs) of the mappings in the LinkedHashMap. We can get the set view and then use the contains() method of the Set interface to check if a particular value exists in the LinkedHashMap.
Syntax
map.entrySet();
The following are the parameters of this function:
- map is the object we create of the LinkedHashMap class
- entrySet is the set view of the mappings in the LinkedHashMap.
Example
In the following example, we are retrieving the SetView object of a LinkedHashMap, comparing the value of each key in it with the key we are searching for:
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
public class CheckIfValueExists {
public static void main(String[] args){
LinkedHashMap<Integer, Integer> map = new LinkedHashMap<>();
map.put(1, 2);
map.put(2, 3);
map.put(3, 4);
map.put(4, 5);
int valueToCheck = 3;
int key = 2;
Set<Map.Entry<Integer, Integer>> entrySet = map.entrySet();
boolean exists = false;
for(Map.Entry<Integer, Integer> entry : entrySet) {
if(entry.getValue() == valueToCheck) {
exists = true;
key = entry.getKey();
break;
}
}
if(exists) {
System.out.println("The value " + valueToCheck + " exists in the LinkedHashMap with key: " + key);
} else {
System.out.println("Value does not exist in the LinkedHashMap");
}
}
}
When you run the above code, it will produce the following output:
The value 3 exists in the LinkedHashMap with key: 2
Using Stream API
Stream API in Java allows us to perform functional-style operations on collections. Functional-style uses lambda expressions to process data. We have methods in the Stream API, and we can chain them together to get the result. You can search for a value in a LinkedHashMap using the streams API as shown below:
map.entrySet().stream().anyMatch(entry -> entry.getKey().equals(key) && !entry.getValue());
Let's break down the above snippet:
- map is the object we create of the LinkedHashMap class
- entrySet() returns a set view of the mappings contained in the map, as we saw in the previous section.
- stream() converts the set view to a Stream.
- anyMatch() is a terminal operation that returns true if any elements of the stream match the provided predicate.
- entry is a Map.Entry object that represents a key-value pair in the map.
- entry.getKey() returns the key of the entry.
- entry.getValue() returns the value of the entry.
Example
In the below example, first we will create a LinkedHashMap of String and Boolean, where each key is the name of a person and the value is a boolean indicating whether that person is available or not.
- Next, we will make a stream of the map entries using the entrySet().stream() method.
- Then we will use the anyMatch() method to check if the specified person exists in the map and is available (i.e., the value is true).
- Then we will print the result based on the value of the boolean variable.
import java.util.LinkedHashMap;
public class CheckIfValueExists {
public static void main(String[] args){
LinkedHashMap<String, Boolean> map = new LinkedHashMap<>();
map.put("Zoro", false);
map.put("Luffy", true);
map.put("Nami", true);
map.put("Ussop", false);
map.put("Sanji", true);
String key = "Nami";
// Check if the person is in the map and is not available
boolean isAvailable = map.entrySet().stream()
.anyMatch(entry -> entry.getKey().equals(key) && entry.getValue());
if(isAvailable) {
System.out.println(key + " is available.");
} else {
System.out.println(key + " is not available.");
}
}
}
When you run the above code, it will produce the following output:
Nami is available.