In Java, remove(key) deletes the mapping for key from a HashMap and returns the value it had, or null if the key was not present. The map shrinks by one when something is removed.
The overload remove(key, value) removes the mapping only if key is currently mapped to value, and returns true or false. It is a safe "delete only if unchanged" operation.
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. The two-argument remove(key, value) was added in Java 8 as a default method of Map, and HashMap overrides it.
Syntax
Two overloads: by key, or by key and expected value.
Java
Map<String, String> phones = new HashMap<>();
phones.put("Bea", "555-0120");
String old = phones.remove("Bea"); // "555-0120"
boolean ok = phones.remove("Bea", "x"); // false (already gone)
Call
What it does
map.remove(k)
Removes k; returns the old value or null.
map.remove(k, v)
Removes only if k maps to v; returns a boolean.
map.keySet().removeIf(test)
Removes every mapping whose key passes the test.
it.remove()
Removes the current entry during an Iterator loop.
Unlike List.remove(int), there is no index version: map.remove(1) on a Map<Integer, V> removes the key 1.
Library declarations
Java
// java.util.HashMap<K, V>
public V remove(Object key)
// Java 8+, default method in Map<K, V>, overridden by HashMap
public boolean remove(Object key, Object value)
Parameters
Parameter
Description
key (Object)
The key whose mapping should be removed. May be null.
value (Object)
Two-argument form only: the value the key must currently have.
Return Value
Return Value
Description
V (one argument)
The previous value, or null if there was no mapping (or it was null).
HashMap iteration order is not guaranteed. This is the typical output on OpenJDK 8–21; other JDKs or versions may print the entries in a different order.
How It Works
Returns what was there.remove("Bea") deletes the entry and hands back Bea's number, so it can still be used or logged.
Missing key is harmless. Zoe was never stored; remove returns null and the map is unchanged.
Size drops by one. Only the Bea mapping was deleted, leaving two contacts.
Printed in hash order. Cy is printed before Amir because of their buckets; the order is not part of the contract.
Example 2 — Free a Seat Only for the Right Person
remove(key, value) deletes a booking only when the seat belongs to the given person.
remove inside for-each -> ConcurrentModificationException
after failed loop: {2=open, 3=done, 4=open}
after removeIf: {2=open, 4=open}
HashMap iteration order is not guaranteed. This is the typical output on OpenJDK 8–21; other JDKs or versions may print the entries in a different order.
How It Works
Small Integer keys. The keys 1 to 4 hash to buckets 1 to 4, so the loop visits them in ascending order on current JDKs.
First removal works. Task 1 is done, and tasks.remove(1) deletes it, but it also bumps the map's modification count.
Next step fails. When the hidden iterator moves on to key 2, it sees the changed count and throws ConcurrentModificationException, so task 3 is never reached and stays in the map.
Safe bulk removal.values().removeIf removes through the iterator internally, deleting task 3 without any exception.
Frequently Asked Questions
Call map.remove(key). It deletes the mapping and returns the old value, or null if the key was not present.
It removes the mapping only if the key is currently mapped to that value, and returns true when it did. It was added in Java 8.
You removed a key while a for-each loop was iterating over the map. Use Iterator.remove() or removeIf() on keySet(), values(), or entrySet().
No. It returns null (or false for the two-argument form) and leaves the map unchanged.
Use map.values().removeIf(v -> v.equals(target)); or entrySet().removeIf for conditions on keys and values.