java.util.HashMap containsKey(Object key)

Description :

On this document we will be showing a java example on how to use the containsKey(Object key) method of HashMap Class. Basically this method is used to check if the key that we have is available on the list of keys on our HashMap object. Generally we need to pass an object as method argument to the containsKey() method. Since everything that we are declaring in java is a subclass of Object, any type is allowed to be passed as method argument. But from our discussion on HashMap we have tackled that the HashMap make use of Generics. So it means that we cannot just pass String on containsKey() method if we have declared Integer as an argument. Lets do a recap on Generics by taking below declaration of HashMap:

HashMap<Integer,String> Studentmap = new HashMap<Integer,String>();

From the above code snippet, the key would be Integer while the value is String. With this Generics in place, the object type we have to pass is restricted to Integer as key while for the value is String. But what if we pass a primitive int as key? Will it give a compilation problem or runtime problem? With the autoboxing in place as of Java 5, this is now possible. If you are interested to learn more about this, please visit the official oracle documentation on Generics.

Getting back to the original topic, this method basically just a way to check if our key is in our HashMap. This is essential if need to check first if its available before doing any business logic.

Important notes for contains(Object key) method:

  • specified by containsKey¬†in interface Map<K,V>
  • overrides containsKey¬†in class AbstractMap<K,V>

Method Syntax :

public boolean containsKey(Object key)

Parameter Input :

[table]
DataType|Parameter|Description
Object|key|the key where which we want check if the HashMap contains it
[/table]

Method Returns :

The containsKey(Object key) method returns true if this map contains a mapping for the specified key.

Compatibility Version :

Requires Java 1.2 and up

Exception :

N/A

Java Code Example :

This java example source code demonstrates the use of containsKey()¬†method of HashMap¬†class. From the example below, basically we have a method init() which actually do the assignment of values to the HashMap object which are expected to be returned by this method. The HashMap returned by the init() method is having an Integer object as key and String object as values. From the main method we put a check if the HashMap contains a key before we retrieve the contents, if the key is not on the database we print a “Not found” message on the console.

Sample Output :

Running the above java example of the usage of HashMap containsKey(Object key) method will give the following output:

HashHamp containsKey() method example

HashHamp containsKey() method example

Similar Method :

The following are the list of method that you can go through for better understanding of the presented example

  • HashMap put()
  • HashMap get()

Suggested Reading List :

References :