Class AbstractSequentialIterator<T>

  • All Implemented Interfaces:

    public abstract class AbstractSequentialIterator<T>
    extends UnmodifiableIterator<T>
    This class provides a skeletal implementation of the Iterator interface for sequences whose next element can always be derived from the previous element. Null elements are not supported, nor is the UnmodifiableIterator.remove() method.


     Iterator<Integer> powersOfTwo =
         new AbstractSequentialIterator<Integer>(1) {
           protected Integer computeNext(Integer previous) {
             return (previous == 1 << 30) ? null : previous * 2;
    12.0 (in Guava as AbstractLinkedIterator since 8.0)
    Chris Povirk
    • Constructor Detail

      • AbstractSequentialIterator

        protected AbstractSequentialIterator​(@CheckForNull
                                             T firstOrNull)
        Creates a new iterator with the given first element, or, if firstOrNull is null, creates a new empty iterator.
    • Method Detail

      • computeNext

        protected abstract T computeNext​(T previous)
        Returns the element that follows previous, or returns null if no elements remain. This method is invoked during each call to next() in order to compute the result of a future call to next().
      • hasNext

        public final boolean hasNext()
        Description copied from interface: java.util.Iterator
        Returns true if the iteration has more elements. (In other words, returns true if would return an element rather than throwing an exception.)
        true if the iteration has more elements
      • next

        public final T next()
        Description copied from interface: java.util.Iterator
        Returns the next element in the iteration.
        the next element in the iteration