notes

An iterator is an object that iterates through objects in a sequence. The developer does not need to know the underlying structure of the sequence.

The iterator() method is the java.lang.Iterable interface, inherited by Collection.

The original

An iterator is a design pattern that is an object that can iterate over and select objects in a sequence without requiring the developer to understand the underlying structure of that sequence. Iterators are often referred to as “lightweight” objects because they are inexpensive to create.

The Iterator function in Java is relatively simple and can only move in one direction:

  1. The iterator() method requires the container to return an iterator. The first time Iterator’s next() method is called, it returns the first element of the sequence. Note: The iterator() method is the java.lang.Iterable interface, inherited by Collection.
  2. Use next() to get the next element in the sequence.
  3. Use hasNext() to check if there are any more elements in the sequence.
  4. Use remove() to remove the element newly returned by the iterator.

Iterator is the simplest implementation of a Java Iterator. ListIterator, designed for a List, has much more functionality, traversing a List in both directions as well as inserting and deleting elements from the List.