Key existence check in HashMap

To check if a key exists in a HashMap in Java, you can use the containsKey method.

Here is an example of how to use this method:

HashMap<String, Integer> map = new HashMap<>();
map.put("key1", 1);
map.put("key2", 2);

if (map.containsKey("key1")) {
   // Key "key1" exists in the map
}

Alternatively, you can use the get method to retrieve the value associated with the key. If the key does not exist in the map, the get method will return null.

Here is an example of how to use this method:

Integer value = map.get("key1");
if (value != null) {
   // Key "key1" exists in the map
}

I hope this helps! Let me know if you have any questions.