map.remove() / containsKey() / containsValue() / size()
These methods remove entries from a map, check whether a specified key or value exists, and retrieve the number of entries or whether the map is empty. They are the fundamental operations for managing map state.
Syntax
// Removes the entry associated with the specified key. map.remove(Object key); // Removes the entry only if both the key and value match (Java 8 and later). map.remove(Object key, Object value); // Returns whether the specified key exists in the map. map.containsKey(Object key); // Returns whether the specified value exists in the map. map.containsValue(Object value); // Returns the number of entries in the map. map.size(); // Returns whether the map is empty. map.isEmpty();
Method List
| Method | Description |
|---|---|
| remove(Object key) | Removes the entry associated with the specified key and returns the removed value. Returns null if the key does not exist. |
| remove(Object key, Object value) | Removes the entry only if both the key and value match. Returns true if the entry was removed. |
| containsKey(Object key) | Returns true if the specified key exists in the map. |
| containsValue(Object value) | Returns true if the specified value exists in the map. Performs a linear O(n) search. |
| size() | Returns the number of key-value pairs in the map. |
| isEmpty() | Returns true if the map contains no entries (size is 0). |
Sample Code
import java.util.HashMap;
HashMap<String, Integer> map = new HashMap<>();
map.put("Alice", 85);
map.put("Bob", 92);
map.put("Carol", 78);
// Use containsKey() to check whether a key exists.
System.out.println(map.containsKey("Alice")); // Outputs: true
System.out.println(map.containsKey("Dave")); // Outputs: false
// Use containsValue() to check whether a value exists.
System.out.println(map.containsValue(92)); // Outputs: true
System.out.println(map.containsValue(50)); // Outputs: false
// Use size() to get the number of entries.
System.out.println(map.size()); // Outputs: 3
// Use isEmpty() to check whether the map is empty.
System.out.println(map.isEmpty()); // Outputs: false
// Use remove() to delete an entry by key.
Integer removed = map.remove("Bob");
System.out.println(removed); // Outputs the removed value: 92
System.out.println(map.size()); // Outputs: 2
// Use remove(key, value) for conditional removal.
boolean result = map.remove("Alice", 100); // Not removed because the value does not match.
System.out.println(result); // Outputs: false
System.out.println(map.containsKey("Alice")); // Outputs: true (the entry remains)
boolean result2 = map.remove("Alice", 85); // Removed because the value matches.
System.out.println(result2); // Outputs: true
Notes
containsKey() uses the hash value to look up the key and runs in O(1) time. containsValue(), however, scans all entries sequentially and runs in O(n) time. If you need to search by value frequently, consider maintaining a reverse map with values as keys, or storing values in a HashSet.
The two-argument remove(key, value) is useful when you need an atomic conditional removal. It is particularly safe to use with ConcurrentHashMap in multithreaded scenarios.
To create a map and add or retrieve entries, see put() / get(). To retrieve lists of keys or values, see keySet() / values() / entrySet().
If you find any errors or copyright issues, please contact us.