Class ZippingIterator<E>

java.lang.Object
org.apache.commons.collections4.iterators.ZippingIterator<E>
All Implemented Interfaces:
Iterator<E>

public class ZippingIterator<E> extends Object implements Iterator<E>
Provides an interleaved iteration over the elements contained in a collection of Iterators.

Given two Iterator instances A and B, the next() method on this iterator will switch between A.next() and B.next() until both iterators are exhausted.

Since:
4.1
  • Field Details

    • iterators

      private final Iterator<Iterator<? extends E>> iterators
      The Iterators to evaluate.
    • nextIterator

      private Iterator<? extends E> nextIterator
      The next iterator to use for next().
    • lastReturned

      private Iterator<? extends E> lastReturned
      The last iterator which was used for next().
  • Constructor Details

    • ZippingIterator

      public ZippingIterator(Iterator<? extends E> a, Iterator<? extends E> b)
      Constructs a new ZippingIterator that will provide interleaved iteration over the two given iterators.
      Parameters:
      a - the first child iterator
      b - the second child iterator
      Throws:
      NullPointerException - if either iterator is null
    • ZippingIterator

      public ZippingIterator(Iterator<? extends E> a, Iterator<? extends E> b, Iterator<? extends E> c)
      Constructs a new ZippingIterator that will provide interleaved iteration over the three given iterators.
      Parameters:
      a - the first child iterator
      b - the second child iterator
      c - the third child iterator
      Throws:
      NullPointerException - if either iterator is null
    • ZippingIterator

      public ZippingIterator(Iterator<? extends E>... iterators)
      Constructs a new ZippingIterator that will provide interleaved iteration of the specified iterators.
      Parameters:
      iterators - the array of iterators
      Throws:
      NullPointerException - if any iterator is null
  • Method Details

    • hasNext

      public boolean hasNext()
      Returns true if any child iterator has remaining elements.
      Specified by:
      hasNext in interface Iterator<E>
      Returns:
      true if this iterator has remaining elements
    • next

      public E next() throws NoSuchElementException
      Returns the next element from a child iterator.
      Specified by:
      next in interface Iterator<E>
      Returns:
      the next interleaved element
      Throws:
      NoSuchElementException - if no child iterator has any more elements
    • remove

      public void remove()
      Removes the last returned element from the child iterator that produced it.
      Specified by:
      remove in interface Iterator<E>
      Throws:
      IllegalStateException - if there is no last returned element, or if the last returned element has already been removed