Package generic.util
Class MergeSortingIterator<T>
java.lang.Object
generic.util.MergeSortingIterator<T>
- Type Parameters:
T- the type of elements in each iterator
- All Implemented Interfaces:
PeekableIterator<T>,Iterator<T>
An iterator which merges sorted iterators according to a comparator
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static classprotected static class -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final Comparator<? super T> protected final PriorityQueue<PeekableIterator<? extends T>> -
Constructor Summary
ConstructorsConstructorDescriptionMergeSortingIterator(Iterable<? extends Iterator<? extends T>> iterators, Comparator<? super T> comparator) Construct a merge sorting iterator -
Method Summary
Modifier and TypeMethodDescriptionbooleanhasNext()next()peek()Returns the item that would be returned by callingIterator.next(), but without incrementing the iterator.static <L,T> MergeSortingIterator <Map.Entry<L, T>> withLabels(Map<L, ? extends Iterator<? extends T>> iterMap, Comparator<T> comparator) Construct a merge-sorting iterator which generates labeled valuesMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface java.util.Iterator
forEachRemaining, remove
-
Field Details
-
comparator
-
queue
-
-
Constructor Details
-
MergeSortingIterator
public MergeSortingIterator(Iterable<? extends Iterator<? extends T>> iterators, Comparator<? super T> comparator) Construct a merge sorting iterator- Parameters:
iterators- a collection of iterators to mergecomparator- the comparator defining how the input and output iterators are sorted
-
-
Method Details
-
withLabels
public static <L,T> MergeSortingIterator<Map.Entry<L,T>> withLabels(Map<L, ? extends Iterator<? extends T>> iterMap, Comparator<T> comparator) Construct a merge-sorting iterator which generates labeled valuesThe map of iterators is a map of entries, each giving a label and an iterator to be merged. Each iterator must return values as sorted by the given comparator. The entries returned by the combined iterator give the values in sorted order, but each has a the key indicating which given iterator returned that value. Note that the returned entry may be re-used by the underlying implementation, so users needing to keep the entry should create a copy.
The purpose of the iterator is to know which iterator provided a given entry in the merged result. While this has general utility, at the moment, it is only used in our tests to verify proper operation of the merge-sorting implementation.
- Parameters:
iterMap- a map of labeled iteratorscomparator- the comparator of values- Returns:
- an iterator which returns labeled values in sorted order
-
hasNext
public boolean hasNext() -
next
-
peek
Description copied from interface:PeekableIteratorReturns the item that would be returned by callingIterator.next(), but without incrementing the iterator.- Specified by:
peekin interfacePeekableIterator<T>- Returns:
- the item that would be returned by calling
Iterator.next() - Throws:
NoSuchElementException- if there is no element to peek at
-