Why is java.util.List.clear () an "optional operation"?

According to the Javadoc :

void clear ()

Removes all of the elements from this list (optional operation). The list will be empty after this call returns.


Why is clearing the list optional?

Doesn't this clear the RAM to clear a list that contains thousands of objects?


source to share

2 answers

It is possible that a List

is immutable.

What if it List

is immutable? You cannot clear an immutable List

, so the method clear()

must be optional.



It has nothing to do with RAM mine, its GC that Garbage ultimately collects this object.

Its more about cases when I obviously can't do my job.

Example, when you try to get a list from an array by calling Arrays.asList (arrayObject), this list cannot be changed. This operation results in a java.lang.UnsupportedOperationException error.



All Articles