Package generic.util

Class MultiIterator<T>

  • Type Parameters:
    T - the type of this iterator
    All Implemented Interfaces:
    java.util.Iterator<T>

    public class MultiIterator<T>
    extends java.lang.Object
    implements java.util.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.

    • Constructor Summary

      Constructors 
      Constructor Description
      MultiIterator​(java.util.List<PeekableIterator<T>> iterators, boolean forward)
      Use this constructor when the items of the iterators are naturally comparable (i.e., they implement Comparable).
      MultiIterator​(java.util.List<PeekableIterator<T>> iterators, java.util.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).
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean hasNext()  
      T next()  
      void remove()  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
      • Methods inherited from interface java.util.Iterator

        forEachRemaining
    • Constructor Detail

      • MultiIterator

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

        public MultiIterator​(java.util.List<PeekableIterator<T>> iterators,
                             java.util.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 Detail

      • remove

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

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

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