Tech and Media Labs
This site uses cookies to improve the user experience.


Java Iterator

Jakob Jenkov
Last update: 2019-01-09

The Java Iterator interface represents an object capable of iterating through a collection of Java objects, one object at a time. The Iterator interface is one of the oldest mechanisms in Java for iterating collections of objects (although not the oldest - Enumerator predated Iterator).

Obtaining an Iterator

Most often that is how you will interact with an Iterator by obtaining it from some Java object that contains multiple nested objects. The standard Java collection interface Collection contains a method called iterator(). By calling iterator() you can obtain an iterator from the given Collection.

You can also obtain an Iterator from many of the Java Collection data structures, e.g. a List, Set, Map, Queue, Deque or Map.

Iterating an Iterator

You iterate the objects in an Iterator using a while loop. Here is an example of iterating the elements of a Java Iterator using a while loop:

Iterator iterator = list.iterator();

while(iterator.hasNext()) {
    Object nextObject = iterator.next();

}

There are two methods to pay attention to in the above Java example. The first method is the Iterator hasNext() method which returns true if the Iterator contains more elements. In other words, if the Iterator has a next element.

The second method to pay attention to is the next() method. The next() method returns the next element of the collection the Iterator traverses.

Iteration Order

The order in which the elements contained in a Java Iterator are traversed depends on the object that supplies the Iterator. For instance, an iterator obtained from a List will iterate through the elements of that List in the same order the elements are stored internally in the List. An Iterator obtained from a Set, on the other hand, does not make any guarantees about the exact sequence the elements in the Set are iterated in.

Java List Iterator

Here is an example of obtaining a Java Set Iterator from a List instance:

List list = new ArrayList();

list.add("123");
list.add("456");
list.add("789");

Iterator iterator = list.iterator();

Java Set Iterator

Here is an example of obtaining a Java Set Iterator from a Set instance:

Set set = new HashSet();

set.add("123");
set.add("456");
set.add("789");

Iterator iterator = set.iterator();

Modification During Iteration

Some collections do not allow you to modify the collection while you are iterating it via an Iterator. In that case you will get a ConcurrentModificationException the next time you call the Iterator next() method. The following example results in a ConcurrentModificationException when executed:

List<String≫ list = new ArrayList<≫();

list.add("123");
list.add("456");
list.add("789");

Iterator<String≫ iterator = list.iterator();

while(iterator.hasNext()) {
    String value = iterator.next();

    if(value.equals("456")){
        list.add("999");
    }
}

The ConcurrentModificationException is thrown because the Iterator gets out of sync with the collection, if you modify the collection while iterating it via the Iterator.

Remove Elements During Iteration

The Java Iterator interface has a remove() method which lets you remove the element just returned by next() from the underlying collection. Calling remove() does not cause a ConcurrentModificationException to be thrown. Here is an example of removing an element from a collection during iteration of its Iterator:

List list = new ArrayList<>();

list.add("123");
list.add("456");
list.add("789");

Iterator iterator = list.iterator();

while(iterator.hasNext()) {
    String value = iterator.next();

    if(value.equals("456")){
        iterator.remove();
    }
        }

Jakob Jenkov




Copyright  Jenkov Aps
Close TOC