In Java, containsKey(key) returns true if a HashMap has a mapping for key, and false otherwise. It does not look at the value at all.
Like get, it hashes the key and checks one bucket, so it is fast on average. It answers a question get cannot: whether a key is stored with a null value or not stored at all.
An instance method of java.util.HashMap (class since Java 1.2): a hash table of key/value pairs with no guaranteed order, one null key, any number of null values, and no thread safety.
Syntax
Pass the key; you get a boolean.
Java
Map<String, String> phones = new HashMap<>();
phones.put("Asha", "555-0101");
boolean a = phones.containsKey("Asha"); // true
boolean b = phones.containsKey("asha"); // false (case-sensitive)
Call
What it does
map.containsKey(k)
true if k has a mapping.
map.containsKey(null)
true if a null key is stored.
!map.containsKey(k)
The key is missing.
map.keySet().contains(k)
Same answer through the key view.
Checking containsKey and then calling get does two lookups. When null values are impossible in your map, a single get plus a null check is enough.
Library declarations
Java
// java.util.HashMap<K, V>
public boolean containsKey(Object key)
// Declared in Map<K, V>
Parameters
Parameter
Description
key (Object)
The key to test. May be null.
Return Value
Return Value
Description
boolean
true if the map contains a mapping for key; otherwise false.
Value-based key.Seat defines equality by row and number and builds its hash from the same two fields with Objects.hash.
New object, same seat. The lookup creates a fresh Seat('B', 7); it lands in the same bucket and equals says it matches, so the answer is true.
Different seat. B8 differs in its number, so no stored key is equal to it.
Identity fallback.RawSeat inherits Object.equals, which only matches the very same object, so even an identical-looking seat is reported as not booked.
Example 3 — Validate Orders Against a Menu
Normalize each order to lower case, add up the prices of items on the menu, and skip the rest.
Java
import java.util.HashMap;
import java.util.Map;
public class HashMapContainsKeyMenu {
public static void main(String[] args) {
Map<String, Integer> menu = new HashMap<>();
menu.put("tea", 30);
menu.put("coffee", 45);
menu.put("cake", 60);
String[] orders = {"coffee", "Tea", "cake", "soup"};
int bill = 0;
for (String item : orders) {
String key = item.toLowerCase();
if (menu.containsKey(key)) {
bill += menu.get(key);
System.out.println("added " + key);
} else {
System.out.println("skipped " + item + " (not on the menu)");
}
}
System.out.println("bill = " + bill);
System.out.println("containsKey(\"Tea\") = " + menu.containsKey("Tea"));
}
}
Output
added coffee
added tea
added cake
skipped soup (not on the menu)
bill = 135
containsKey("Tea") = false
How It Works
Normalize first. Orders are converted with toLowerCase() so "Tea" matches the stored key "tea".
Guard the unboxing. Prices are added only after containsKey confirms the item, so menu.get(key) never returns null into bill +=.
Reject unknown items. "soup" is not a key, so it is reported and contributes nothing: 45 + 30 + 60 = 135.
Raw key is case-sensitive. The final line shows that without normalizing, "Tea" is not found.
Frequently Asked Questions
Call map.containsKey(key). It returns true when the key has a mapping, even if the value is null.
They differ only for keys mapped to null: containsKey() returns true, while get() returns null as if the key were missing.
Yes. String.equals is case-sensitive, so "Tea" and "tea" are different keys. Normalize the case before storing and looking up.
The class probably does not override equals() and hashCode(). Without them, only the exact same object is found.
Constant time on average, like get(), because only one bucket is examined.