In below example we are going to Check key exists in Hashmap java
Syntax
Example
import java.util.HashMap;
import java.util.Map;
public class KeyExistsHashMapExample {
public static void main(String[] args) {
Map<String, String> hashMap = new HashMap<String, String>();
hashMap.put("1", "Car");
hashMap.put("2", "Bus");
hashMap.put("3", "Train");
boolean isKeyAvaillable = hashMap.containsKey("1");
if(isKeyAvaillable) {
System.out.println("The value is "+hashMap.get("1"));
}
else {
System.out.println("Key is not availlable");
}
}
}
Output
The value is Car
Reference
https://beginnersbug.com/hashmap-in-java/
https://docs.oracle.com/javase/8/docs/api/java/util/Map.html#containsKey-java.lang.Object-