Search code examples
javalistiterator

Do the List iterators support remove?


The remove method is optional in Iterator and ListIterator. Do the Iterators or ListIterators you get from the JDK lists implement the remove operation? This is not stated in the documentation.


Solution

  • In summary:

    • ArrayList.iterator supports remove even though it's not explicitly documented
    • LinkedList.iterator supports remove even though it's not explicitly documented
    • CopyOnWriteArrayList.iterator does not support remove
    • List returned by Collections.unmodifiableList does not support remove

    The rule of thumb for the standard classes seems to be: iterators support removal unless there's a good reason not to.