Class PeekingIterator<E>

    • Constructor Detail

      • PeekingIterator

        public PeekingIterator​(ListIterator<E> iterator)
        Constructor.
        Parameters:
        iterator - the iterator to decorate
      • PeekingIterator

        public PeekingIterator​(List<E> list)
        Constructor.
        Parameters:
        list - the provider of the iterator to decorate
    • Method Detail

      • peekingIterator

        public <E> PeekingIterator<E> peekingIterator​(ListIterator<E> iterator)
        Decorates the specified iterator to support one-element lookahead.

        If the iterator is already a PeekingIterator it is returned directly.

        Type Parameters:
        E - the element type
        Parameters:
        iterator - the iterator to decorate
        Returns:
        a new peeking iterator
        Throws:
        NullPointerException - if the iterator is null
      • peek

        public E peek()
        Returns the next element in iteration without advancing the underlying iterator. If the iterator is already exhausted, null will be returned.

        Note: this method does not throw a NoSuchElementException if the iterator is already exhausted. If you want such a behavior, use element() instead.

        The rationale behind this is to follow the Queue interface which uses the same terminology.

        Specified by:
        peek in interface LookaheadIterator<E>
        Returns:
        the next element from the iterator
      • element

        public E element()
        Returns the next element in iteration without advancing the underlying iterator. If the iterator is already exhausted, null will be returned.
        Specified by:
        element in interface LookaheadIterator<E>
        Returns:
        the next element from the iterator
        Throws:
        NoSuchElementException - if the iterator is already exhausted according to hasNext()
      • currentIndex

        public int currentIndex()