Check if a particular key exists in HashMap in Java

In this tutorial, we will learn how to Check if a particular key exists in HashMap in Java ?

HashMap in Java is a hash table based implementation of Map Interface. HashMap extends AbstractMap and implements Serializable, Cloneable and Map interfaces. It is used to store key-value pairs. HashMap is a widely used collection classes in java.  To access a value in HashMap, you need to have a key.

To check if a particular key exists in HashMap or not, HashMap class in Java provides a method containsKey(). We will use this method to check if particular key exists in HashMap. The syntax of this method is:

Syntax of containsKey() method:

Parameters:
key – The key whose presence in this map is to be tested.

Returns:
true if the key exists in map.

Example:

 

Output:

Related posts

Leave a Comment