Java Check if key exists in HashMap Example

This example shows how to check if key exists in HashMap in Java using the get and containsKey methods of the HashMap class. The example also shows how to check if HashMap has the key using the best approach.

How to check if the key exists in HashMap in Java?

There are a couple of ways using which you can check if the HashMap contains a key.

1. How to check if HashMap contains the key using the get method?

The get method of the HashMap class returns the value mapped to a given key in the map. If the key is not mapped to any value in the map, it returns null.

You can compare the return value with null to check if the HashMap has the key.

Output

Important Note:

This approach works only if the HashMap does not contain null values mapped to the keys. If the HashMap contains null values, the get method returns null for those keys as given below.

Output

As you can see from the output, even if the HashMap has the key 2, our program printed “Key does not exist” because it was mapped to a null value.

2. How to check If HashMap has the key using the containsKey method?

The containsKey method of the HashMap class returns true if the HashMap contains the specified key, false otherwise.

The containsKey method returns true even if the key is mapped to a null value as given below.

Output

Which method to use? get vs containsKey method performance

Let’s look at the source code of both of these methods first.

As far as performance is concerned, there is not much difference between these two methods. However, the suggested way to check if the key exists in the HashMap is to use the containsKey method because of these two reasons.

1. If the map contains the specified key mapped to a null value, the get method approach will give a false result. The containsKey method returns true even if the key is mapped to a null value.

2. Usage of containsKey method makes code more readable as it clearly states the purpose of the code.

How to check if a key exists if the key is an object of a custom class?

Let’s have a look at below given example in which map keys are objects of the custom class Empl.

Output

Even though the map contained the key mapped to a value, the containsKey method returned false. It is because our Empl class has not overridden the equals and hashCode methods. Any custom class whose object is used as the key of the map needs to override these two methods for the containsKey method to work correctly.

Let’s override these two methods in our Empl class and see if the containsKey method works as expected.

Output

This example is part of the HashMap in Java tutorial.

Please let me know your views in the comments section below.

About the author

RahimV

RahimV

My name is RahimV and I have over 16 years of experience in designing and developing Java applications. Over the years I have worked with many fortune 500 companies as an eCommerce Architect. My goal is to provide high quality but simple to understand Java tutorials and examples for free. If you like my website, follow me on Facebook and Twitter.

Add Comment

Your email address will not be published. Required fields are marked *