The example aims to check if a HashMap object contains a particular key by containsKey method in Java.
Source Code
package com.beginner.examples;
import java.util.HashMap;
public class CheckHashMapContainsKeyExample {
public static void main(String[] args) {
// Create a HashMap object
HashMap map = new HashMap();
// Put Key-value
map.put("1", "Jan");
map.put("2", "Feb");
map.put("3", "Mar");
map.put("4", "Apr");
map.put("5", "May");
// The return value of this method is of a Boolean type
//Returns true if it contains this key,or false if it does not
boolean b = map.containsKey("3");
boolean b1 = map.containsKey("6");
System.out.println("Key 3:" + b + "nKey 6:" + b1);
}
}
Output:
Key 3:true
Key 6:false
References
Imported packages in Java documentation: