12

As per Sun ,

"Iterator.remove is the only safe way to modify a collection during iteration; the behavior is unspecified if the underlying collection is modified in any other way while the iteration is in progress."

I have two questions :

  1. What makes this operation "Iterator.remove()" stable than the others ?
  2. Why did they provide a "Collection.remove()" method if it will not be useful in most of the use-cases?
AllTooSir
  • 48,828
  • 16
  • 130
  • 164

6 Answers6

16

First of all, Collection.remove() is very useful. It is applicable in a lot of use cases, probably more so than Iterator.remove().

However, the latter solves one specific problem: it allows you to modify the collection while iterating over it.

The problem solved by Iterator.remove() is illustrated below:

    List<Integer> l = new ArrayList<Integer>(Arrays.asList(1, 2, 3, 4));
    for (int el : l) {
        if (el < 3) {
            l.remove(el);
        }
    }

This code is invalid since l.remove() is called during iteration over l.

The following is the correct way to write it:

    Iterator<Integer> it = l.iterator();
    while (it.hasNext()) {
        int el = it.next();
        if (el < 3) {
            it.remove();
        }
    }
NPE
  • 486,780
  • 108
  • 951
  • 1,012
9

If you're iterating over a collection and use:

Collection.remove() 

you can get runtime errors (specifically ConcurrentModifcationException) because you're changing the state of the object used previously to construct the explicit series of calls necessary to complete the loop.

If you use:

Iterator.remove()

you tell the runtime that you would like to change the underlying collection AND re-evaluate the explicit series of calls necessary to complete the loop.

Slartibartfast
  • 1,605
  • 2
  • 16
  • 23
8

As the documentation you quoted clearly states,

Iterator.remove is the only safe way to modify a collection during iteration

(emphasis added)

While using am iterator, you cannot modify the collection, except by calling Iterator.remove().

If you aren't iterating the collection, you would use Collection.remove().

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
2

What makes this operation "Iterator.remove()" stable than the others ?

It means that iterator knows you removed the element so it won't produce a ConcurrentModifcationException.

Why did they provide a "Collection.remove()" method if it will not be useful in most of the use-cases ?

Usually you would use Map.remove() or Collection.remove() as this can be much more efficient than iterating over every objects. If you are removing while iterating often I suspect you should be using different collections.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
0
  1. Is just a design choice. It would have been possible to specify a different behavior (i.e. the iterator has to skip values that were removed by Collection.remove()), but that would have made the implementation of the collection framework much more complex. So the choice to leave it unspecified.

  2. It's quite useful. If you know the object you want to remove, why iterate?

Chris
  • 4,133
  • 30
  • 38
0

From what I understand, the Collection.remove(int index) will also return the removed object. Iterative.remove() will not.

Kgi
  • 1
  • 1