In the last tutorial we learnt how to check whether a particular value exists in HashMap. In this example we are gonna see how to check if a particular key is present in HashMap. We will be using containsKey() method of HashMap class to perform this check. The method definition and description are as follows:
public boolean containsKey(Object key): Returns true if this map contains a mapping for the specified key.
Example
The steps we followed in the below example are:
1) Create a HashMap and populate it with key-value pairs.
2) Check any key existence by calling containsKey() method. This method returns a boolean value.
import java.util.HashMap;
public class CheckKeyExample {
public static void main(String[] args) {
// Creating a HashMap of int keys and String values
HashMap<Integer, String> hashmap = new HashMap<Integer, String>();
// Adding Key and Value pairs to HashMap
hashmap.put(11,"Chaitanya");
hashmap.put(22,"Pratap");
hashmap.put(33,"Singh");
hashmap.put(44,"Rajesh");
hashmap.put(55,"Kate");
// Checking Key Existence
boolean flag = hashmap.containsKey(22);
System.out.println("Key 22 exists in HashMap? : " + flag);
boolean flag2 = hashmap.containsKey(55);
System.out.println("Key 55 exists in HashMap? : " + flag2);
boolean flag3 = hashmap.containsKey(99);
System.out.println("Key 99 exists in HashMap? : " + flag3);
}
}
Output:
Key 22 exists in HashMap? : true Key 55 exists in HashMap? : true Key 99 exists in HashMap? : false
Leave a Reply