Iterator interface in Java

Difference between Enumeration and Iterator

Although both interfaces are designed to traverse collections, there are significant differences between them:

  • you cannot add/remove elements using Enumeration;
  • fixed method names in Iterator to improve code readability (Enumeration.hasMoreElements() corresponds to Iterator.hasNext(), Enumeration.nextElement() corresponds to Iterator.next(), etc.);
  • Enumeration is found in legacy classes such as Vector/Stack, while Iterator is found in all modern collection classes.

How are Iterable and Iterator related

The Iterable interface has only one method, iterator(), which returns an Iterator.

How are Iterable, Iterator, and "for-each" related

Classes that implement the Iterable interface can be used in a for-each construct that uses an Iterator.

Comparison of Iterator and ListIterator

  • ListIterator extends the Iterator interface;
  • ListIterator can only be used to iterate over the elements of the List collection;
  • The Iterator allows you to iterate over elements in one direction only, using the next() method. Whereas ListIterator allows you to iterate over the list in both directions using the next() and previous() methods;
  • The ListIterator does not point to a specific element: its current position is between the elements returned by the previous() and next() methods.
  • With the ListIterator you can modify the list by adding/removing elements using the add() and remove() methods. The Iterator does not support this functionality.

What happens when Iterator.next() is called without first calling Iterator.hasNext()

If the iterator points to the last element of the collection, a NoSuchElementException will be thrown, otherwise the next element will be returned.

How many elements will be skipped if Iterator.next() is called after 10 calls to Iterator.hasNext()?

Not at all - hasNext() only checks for the next element.

How does the collection behave when iterator.remove() is called?

If the call to iterator.remove() was preceded by a call to iterator.next(), then iterator.remove() will remove the collection item pointed to by the iterator, otherwise an IllegalStateException() will be thrown.

How does an already instantiated iterator for collection behave when collection.remove() is called?

The next call to the iterator methods will throw a ConcurrentModificationException.

How to avoid ConcurrentModificationException while iterating over a collection?

  • Try to find another fail-safe iterator. For example, you can use ListIterator for List.
  • Use ConcurrentHashMap and CopyOnWriteArrayList.
  • Convert list to array and iterate over the array.
  • Block changes to the list while iterating with a synchronized block.

The negative side of the last two options is performance degradation.

Which collection implements the FIFO service discipline?

FIFO, First-In-First-Out ("first in, first out") - this is the principle behind the Queue collection.

Which collection implements the FILO service discipline?

FILO, First-In-Last-Out - the Stack collection is built on this principle.


Read also:


Comments

Popular posts from this blog

Methods for reading XML in Java

XML, well-formed XML and valid XML

ArrayList and LinkedList in Java, memory usage and speed