Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why iterator.remove() has been described as optional operation?

Tags:

java

iterator

I went through the documentation(http://java.sun.com/javase/6/docs/api/java/util/Iterator.html) of Iterator.remove() there remove() was described as

void remove()

Removes from the underlying collection the last element returned by the iterator (optional operation). This method can be called only once per call to next. The behavior of an iterator is unspecified if the underlying collection is modified while the iteration is in progress in any way other than by calling this method.

  1. So can anybody tell what "optional" means.
  2. Does this affect the robustness of operation?(Like c++ ,it does not guarantee the robustness of the operations.)
  3. Why "optional" has been specified categorically here.
  4. What does "modification" mean in the second line of documentation

behavior of an iterator is unspecified if the underlying collection is modified

like image 227
Ashish Agarwal Avatar asked Oct 08 '09 07:10

Ashish Agarwal


People also ask

Why iterator has remove method?

An element can be removed from a Collection using the Iterator method remove(). This method removes the current element in the Collection. If the remove() method is not preceded by the next() method, then the exception IllegalStateException is thrown.

What is difference between remove () method of collection and remove () method of iterator?

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."

What does iterator () do in Java?

Iterator in Java is used to traverse each and every element in the collection. Using it, traverse, obtain each element or you can even remove. ListIterator extends Iterator to allow bidirectional traversal of a list, and the modification of elements. The iterator() method is provided by every Collection class.

What is an iterator implemented a different way for every collection?

Iterator must be used whenever we want to enumerate elements in all Collection framework implemented interfaces like Set, List, Queue, Deque, and all implemented classes of Map interface. Iterator is the only cursor available for the entire collection framework.


2 Answers

#1: Optional means you can implement it or throw an UnsupportedOperationException

#2: This operation is optional because sometimes you just don't want your iterator's content to be modified. Or what do you understand by "robustness of operation"?

EDIT #4: behavior of an iterator is unspecified if the underlying collection is modified

Normally, you use an iterator by executing

List<String> c = new ArrayList<String>();
c.add("Item 1");
c.add("Item 2");
c.add("Item 3");
...
for (Iterator<String> i = c.iterator(); i.hasNext();)
{
  String s = i.next();
  ...
}

If you now would want to remove an item while iterating through the list, and you would call

c.remove("Item 2");

this is not clean, possibly corrupts data in your List/Collection/... and should be avoided. Instead, remove() the item through the iterator:

i.remove();
like image 106
Atmocreations Avatar answered Sep 21 '22 21:09

Atmocreations


First of all java.util.Iterator is an interface i.e. an agreement how classes that implement this interface interract with the rest of the world. It's their responsibility how they'll implement interaface's methods.

If the underlying data structure doesn't allow removal then remove() will throw an UnsupportedOperationException. For example, if you are iterating through a result set retrieved from a DB it does make sense not to implement this method.

If you iterate over some collection which is shared between concurrent threads and the other thread modifies the data iterating thread then will return undeterministic results.

like image 28
Boris Pavlović Avatar answered Sep 21 '22 21:09

Boris Pavlović