Check if a particular key exists in Java LinkedHashMap



LinkedHashMap is a data structure in Java that is part of the Java Collections Framework. It is very similar to HashMap.It stores key-value pairs, where each key is unique, and it allows null values, but allows only one null key.

The main difference is that the LinkedHashMap returns the elements in the order they were inserted. This is because it maintains a linked list with entries in the map, which helps it to retain the insertion order of the elements.

Our task is to check if a particular key exists in a LinkedHashMap in Java.

Scenario 1

Following is a scenario where we will check if the key=2 exists in a LinkedHashMap or not:

Input: {1=Apple, 2=Banana, 3=Cherry}, key: 2
Output: Key exists in the LinkedHashMap and its value is: Banana
Explanation: The key 2 exists in the LinkedHashMap and its corresponding value is Banana.

Scenario 2

Following is a scenario where we will check if the key=4 exists in a LinkedHashMap or not:

Input: {1=Apple, 2=Banana, 3=Cherry}, key: 4
Output: Key does not exist in the LinkedHashMap
Explanation: The key 4 does not exist in the LinkedHashMap.

We can check if a particular key exists in a Java LinkedHashMap in the following ways:

Using containsKey() Method

The containsKey() is a method of the LinkedHashMap class, which is inherited from the Map interface. It accepts a key (object) as a parameter and verifies whether the current map contains the given key. If the key exists, this method returns true; otherwise, it returns false.

Syntax

The following is the syntax of the containsKey() method:

map.containsKey(key);

Example

In the following example, we will create a LinkedHashMap of Integer and String types, and store numbers as keys and their corresponding names as values. We will then check if a particular key exists in the LinkedHashMap using the containsKey() method.

import java.util.LinkedHashMap;
public class CheckIfKeyExists {
   public static void main(String[] args){
      LinkedHashMap <Integer, String> map = new LinkedHashMap<>();
      map.put(1, "Apple");
      map.put(2, "Banana");
      map.put(3, "Cherry");

      int keyToCheck = 2;

      if(map.containsKey(keyToCheck)) {
        //map.get() is method to get the value associated with the key
         System.out.println("Key exists in the LinkedHashMap and its value is: " + map.get(keyToCheck));
      } else {
         System.out.println("Key does not exist in the LinkedHashMap");
      }
   }
}

When you run the above code, it will produce the following output:

Key exists in the LinkedHashMap and its value is: Banana

Using keySet() Method

This keySet() method returns the set of keys present in a LinkedHashMap. Instead of searching the LinkedHashMap, we can check if the key exists in its keyset using the contains() method of the Set interface.

Syntax

Following is the syntax of the keySet() method:

map.keySet();

Example

In the following example, we are retrieving the keyset of a LinkedHasMap and verifying if the required key exists in it:

import java.util.LinkedHashMap;
import java.util.Set;
public class CheckIfKeyExists {
   public static void main(String[] args){
      LinkedHashMap <Integer, String> map = new LinkedHashMap<>();
      map.put(1, "Apple");
      map.put(2, "Banana");
      map.put(3, "Cherry");   

      int keyToCheck = 4;
      Set<Integer> keys = map.keySet();

      if(keys.contains(keyToCheck)) {
         System.out.println("Key exists in the LinkedHashMap and its value is: " + map.get(keyToCheck));
      } else {
         System.out.println("Key does not exist in the LinkedHashMap");
      }
   }
}

When you run the above code, it will produce the following output:

Key does not exist in the LinkedHashMap

Using Streams API

The Streams API was introduced in Java8. It provides methods that help us to process collections. We can use the anyMatch() method of the Stream interface to check if a particular key exists in the LinkedHashMap.

Example

In the following example, we are verifying for a key in the LinkedHashMap using the Streams API:

import java.util.LinkedHashMap;
import java.util.stream.Stream;
public class CheckIfKeyExists {
   public static void main(String[] args){
      LinkedHashMap<Integer, String> map = new LinkedHashMap<>();
      map.put(1, "Apple");
      map.put(2, "Banana");
      map.put(3, "Cherry");

      int keyToCheck = 3;

      boolean exists = map.keySet().stream().anyMatch(key -> key == keyToCheck);

      if(exists) {
         System.out.println("Key exists in the LinkedHashMap and its value is: " + map.get(keyToCheck));
      } else {
         System.out.println("Key does not exist in the LinkedHashMap");
      }
   }
}

When you run the above code, it will produce the following output:

Key exists in the LinkedHashMap and its value is: Cherry
Aishwarya Naglot
Aishwarya Naglot

Writing clean code… when the bugs aren’t looking.

Updated on: 2025-07-25T15:49:50+05:30

600 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements