Before calling .get(), you often want to check if a key exists. The .containsKey() method returns true if the map contains that key and false otherwise:
if (ages.containsKey("Alice")) {
int age = ages.get("Alice");
System.out.println(age);
}
There is also .containsValue(), which checks whether a value exists anywhere in the map. The difference in performance matters: .containsKey() runs in average time and space because it hashes the key directly. .containsValue() runs in time and space because it has to scan every entry.
Use .containsKey() as your default guard. Reach for .containsValue() only when you have no other option.