Class MergingIterator<T>

  • All Implemented Interfaces:
    CloseableIterator<T>, Closeable, AutoCloseable, Iterator<T>

    public class MergingIterator<T>
    extends Object
    implements CloseableIterator<T>
    An iterator over Iterators that return Ts. Calling next() returns the next T ordered according to Comparator provided at construction time. Importantly, the elements in the input Iterators must already be sorted according to the provided Comparator.
    • Constructor Detail

      • MergingIterator

        public MergingIterator​(Comparator<T> comparator,
                               Collection<CloseableIterator<T>> iterators)
        Creates a MergingIterator over the given Collection of iterators whose elements will be returned in the order defined by the given Comparator.
    • Method Detail

      • hasNext

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

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

        public void remove()
        Unsupported.
        Specified by:
        remove in interface Iterator<T>
      • close

        public void close()
        Closes every CloseableIterator in this MergingIterator. After calling, calls to hasNext() will always return false.
        Specified by:
        close in interface AutoCloseable
        Specified by:
        close in interface Closeable
        Specified by:
        close in interface CloseableIterator<T>