In Java, clear() removes every key/value mapping from a HashMap. Afterwards size() is 0, isEmpty() is true, and every lookup returns null.
The map object itself survives, so you can keep adding to it. Every variable that refers to the same map, and every view such as keySet(), sees the cleared state. A copy made earlier keeps its own entries.
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.
Points the variable at a new map; other references keep the old one.
map.keySet().clear()
Same effect through a view.
map.isEmpty()
true right after clear().
In OpenJDK, clear() empties the internal table but keeps its current length, so a map that once grew large keeps that capacity. Creating a new map is the way to release it; either way this is an implementation detail.
Library declarations
Java
// java.util.HashMap<K, V>
public void clear()
// Declared in Map<K, V>
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
Three items. The cart prints its mappings in hash order with a size of 3.
All gone. After clear() the map prints as {} and its size is 0.
Lookups return null.get("milk") no longer finds anything, and isEmpty() confirms the map is empty.
Same object, ready again.clear() does not destroy the map, so put("tea", 1) works on the very same cart.
Example 2 — clear() Affects Every Reference, Not Copies
Clear a settings map that is also referenced by a second variable, a key view, and an independent copy.
Two names, one map.sameMap = settings copies only the reference, so both variables point at the same HashMap.
A real copy.new HashMap<>(settings) builds a separate map holding its own two mappings.
Clear through either name. After settings.clear(), sameMap reports size 0 and the key view taken earlier is empty too.
Copy untouched. The snapshot still has both settings, so snapshot.get("theme") returns dark.
Example 3 — Reuse One Map for Each Day's Sales
Count each day's items in the same map, clearing it at the start of every day.
Java
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
public class HashMapClearBatches {
public static void main(String[] args) {
String[][] days = {
{"pen", "ink", "pen"},
{"ink", "pad"}
};
Map<String, Integer> sold = new HashMap<>();
for (int d = 0; d < days.length; d++) {
sold.clear();
for (String item : days[d]) {
sold.merge(item, 1, Integer::sum);
}
System.out.println("day " + (d + 1) + ": " + new TreeMap<>(sold));
}
}
}
Output
day 1: {ink=1, pen=2}
day 2: {ink=1, pad=1}
How It Works
One map, many batches.sold is created once outside the loop and cleared at the start of each day.
Fresh counts. Without the clear(), day 2 would report ink=2 and still list two pens from day 1.
Counting.merge(item, 1, Integer::sum) inserts 1 for a new item and adds 1 for a repeat, so day 1 has pen=2.
Readable order. Each day's counts are printed through a sorted TreeMap copy.
Frequently Asked Questions
Call map.clear(). The map becomes empty and can be reused.
clear() empties the existing object, which every reference sees. Assigning a new map only changes that one variable; other references keep the old entries.
No. The copy is a separate map with its own entries.
Not in OpenJDK: the table keeps its length and only the entries are removed. Create a new map if you need to release that memory.