views:

316

answers:

3

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 any body tell whats this "optional" means.
  2. Does this affect the robustness of operation(Like c++ ,it does not guarantees the robustness of the operations.)
  3. Why "optional" has been specified categorically here.
  4. in second line of documentation

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

whats modification really means here?

+8  A: 

#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();
Atmocreations
@Atmocreation:thanx for the explanation,can you please describe the 4 question?
Sam Rudolph
added explanation about removal, read on from **EDIT** #4
Atmocreations
@Atmocreation:Thanx for the explanation ...its worth..
Sam Rudolph
+2  A: 

It is described as being optional because not all collection classes that can give you an iterator implement the remove() method in the iterator they return. If the returned iterator doesn't implement it, an UnsupportedOperationException will be thrown.

The normal java.util.ArrayList, java.util.LinkedList and other standard collection classes all implement the remove() method in their iterators, so you can use it safely.

Jesper
+4  A: 

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.

Boris Pavlović
but the fact is every implementing method allows the removal?
Sam Rudolph
@Boris:thanx for explanation.can you please describe the 4 question?
Sam Rudolph