clear() removes every element from a Java collection; after a successful call, that collection is empty. The operation is optional, however, so an implementation that does not support clearing may throw UnsupportedOperationException. The Java SE Collection API defines the contract, while the specific collection class determines implementation details such as performance.
What does clear() do?
Collection.clear() has a void return type and removes all elements from the collection on which it is called. It does not return a new collection. After the method completes successfully, the receiver is empty, as specified by the Collection API.
List<String> names = new ArrayList<>(List.of("Ada", "Linus"));
names.clear();
assert names.isEmpty();
The assertion illustrates the documented postcondition; it is not a claim about a separately measured test. clear() changes the existing object, so other variables that refer to that same object also observe it as empty after a successful call.
How does clear() work on lists, sets, and maps?
Lists and sets
List and Set extend Collection, so both provide the inherited clear() contract. It removes all list elements or set members; the method does not choose particular entries to retain. The Set API states that the set is empty after a supported call returns.
Set<Integer> ids = new HashSet<>(Set.of(10, 20));
ids.clear();
assert ids.isEmpty();
Maps
A Map is not a Collection: it stores key/value mappings rather than collection elements. Its own clear() method removes all mappings. For example, the Map API defines the map-specific operation.
Map<String, Integer> scores = new HashMap<>();
scores.put("Ada", 100);
scores.clear();
assert scores.isEmpty();
Why can clear() throw UnsupportedOperationException?
Clearing is an optional operation in the collection interfaces. A collection whose implementation does not support structural removal may reject the call with UnsupportedOperationException. This can occur with unmodifiable collections, fixed-size views, or other restricted implementations; the exact behavior depends on the concrete type and view. Check that implementation’s documentation rather than assuming every object exposed as a list or set is mutable. The optional-operation contract is documented by the Collection API and, for sets, the Set API.
Rank #2
How is clear() different from removeAll() or a new collection?
| Operation | Effect | When to use it |
|---|---|---|
clear() |
Removes every element from the receiver. | Use when the existing collection object should become empty. |
remove(Object) |
Removes one matching element, if present. | Use to remove a particular element. |
removeAll(Collection) |
Removes receiver elements that are also contained in the supplied collection. | Use to remove a selected group rather than everything. |
removeIf(predicate) |
Removes elements that satisfy the predicate. | Use when the removal rule is expressed as a condition. The default Collection implementation traverses using an iterator and calls Iterator.remove(). |
| Assign a new collection | Changes the variable to reference another object; it does not empty the old object. | Use when only that variable should refer to a fresh collection and aliases to the old one should remain unchanged. |
For example, if list and alias refer to the same list, calling list.clear() empties the shared object. Assigning list = new ArrayList<>() instead redirects only list; alias still refers to the original collection. The removal methods’ contracts are described in the Collection API.
Does clear() release a collection’s memory?
The interface contract guarantees an empty collection after a successful call, not a particular time complexity, backing-storage policy, or memory-retention outcome. A concrete class may override clear() and define its own implementation behavior. For example, the ArrayList API documents that class’s behavior; do not infer a universal capacity-shrinking or memory-release guarantee from the interface method alone.
Free tools Windows power users keep installed
One-click scans. No signup required.
Quick Recap
Best Value
Rank #4
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

