Package generic.util

Class MultiIterator<T>

java.lang.Object
generic.util.MultiIterator<T>
Type Parameters:
T - the type of this iterator
All Implemented Interfaces:
Iterator<T>

public class MultiIterator<T> extends Object implements Iterator<T>
An iterator that is comprised of one or more PeekableIterators. The type T of the the iterators must either implement Comparable directly or you must provide a Comparator for comparing the types. Further, it is assumed that the iterators return values in sorted order. If the sorted order is reversed, then that must be indicated in the constructor of this class.

This class allows duplicate items in the iterators. Thus, if you do not wish to process duplicate values, then you need to de-dup the data returned from next(). Alternatively, you could subclass this iterator and de-dup the returned values.

This class also does not handle null items returned during the iteration process.

  • Field Details

  • Constructor Details

    • MultiIterator

      public MultiIterator(List<PeekableIterator<T>> iterators, boolean forward)
      Use this constructor when the items of the iterators are naturally comparable (i.e., they implement Comparable).
      Parameters:
      iterators - the iterators that provide the data
      forward - true if the iterators provide data sorted ascending; false for descending
    • MultiIterator

      public MultiIterator(List<PeekableIterator<T>> iterators, Comparator<T> comparator, boolean forward)
      Use this constructor when the items of the iterators are not naturally comparable (i.e., they do not implement Comparable).
      Parameters:
      iterators - the iterators that provide the data
      comparator - the comparator used to find the next item
      forward - true if the iterators provide data sorted ascending; false for descending
  • Method Details

    • remove

      public void remove()
      Specified by:
      remove in interface Iterator<T>
    • hasNext

      public boolean hasNext()
      Specified by:
      hasNext in interface Iterator<T>
    • next

      public T next()
      Specified by:
      next in interface Iterator<T>