Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator

What is the ConcurrentHashMap.containsKey() method in Java?

Programming Bytes

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

ConcurrentHashMap is a thread-safe version of a HashMap that allows concurrent read and thread-safe update operation. ConcurrentHashMap internally uses Hashtable.

The ConcurrentHashMap object is divided into multiple portions according to the concurrency level. Only a specific portion of Map is locked during an update operation, instead of locking the whole Map. Since it doesn’t lock the whole Map, there may be a chance of read overlapping with update operations like put() and remove(). In that case, the result of the get() method will reflect the most recently completed operation from their start. Additionally, null is not allowed as a key or value.

The containsKey() method

The containsKey() method of ConcurrentHashMap is used to check if a value is mapped to the specified key.

Syntax


public boolean containsKey(Object key)

Parameters

The containsKey method accepts the key that is checked for presence as a parameter.


Note: If we pass null as an argument, then the NullPointerException is thrown.

Return value

This method returns true if the key has a mapping. Otherwise, it returns false.

Code

The example below shows how to use the containsKey() method.

import java.util.concurrent.ConcurrentHashMap;
class ContainsKey {
public static void main( String args[] ) {
ConcurrentHashMap<Integer, String> map = new ConcurrentHashMap<>();
map.put(1, "one");
map.put(2, "two");
System.out.println("Checking if the key '1' has mapping: "+ map.containsKey(1));
System.out.println("Checking if the key '3' has mapping: "+ map.containsKey(3));
}
}

Explanation

In the code above:

  • Line 1: We import the ConcurrentHashMap class.

  • Line 2: We create a ConcurrentHashMap object with the name map.

  • Lines 5 and 6: We use the put method to add two mappings ({1=one, 2=two}) to the map object.

  • Line 7: We use the containsKey method to check if the map has a mapping for the key 1. We get true as a result because the map has a mapping for the key 1.

  • Line 8: We use the containsKey method to check if the map has a mapping for the key 3. The method returns false because the map does not have mapping for the key 3.

RELATED TAGS

java
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring