@Test public void testMergingIterator() { Comparator<Integer> cmp = new Comparator<Integer>() { @Override public int compare(final Integer o1, final Integer o2) { return Integer.compare(o1, o2); } }; @SuppressWarnings("unchecked") Iterator<Integer>[] its = new Iterator[]{ Arrays.asList(new Integer[]{1, 4, 7, 10}).iterator(), Arrays.asList(new Integer[]{2, 5, 8, 11}).iterator(), Arrays.asList(new Integer[]{3, 6, 9, 12}).iterator() }; MergingIterator<Integer> merge = new MergingIterator<>(cmp, its); int i = 1; while (merge.hasNext()) { Assert.assertEquals(i, (int) merge.next()); i++; } Assert.assertEquals(13, i); }
public MergingIterator(final Comparator<T> c, final Iterator<T>[] its) { this.heap = new PriorityQueue<>(11, new TupleKeyComparator<T, Iterator<T>>(c)); for (final Iterator<T> it : its) { final T b = it.hasNext() ? it.next() : null; if (b != null) { heap.add(new Tuple<>(b, it)); } } }
MergingIterator implements Iterator<T> { public MergingIterator(final Comparator<T> c, final Iterator<T>[] its) { this.heap = new PriorityQueue<>(11, new TupleKeyComparator<T, Iterator<T>>(c)); for (final Iterator<T> it : its) { final T b = it.hasNext() ? it.next() : null; if (b != null) { heap.add(new Tuple<>(b, it)); } } } }
MergingIterator implements Iterator<T> { public MergingIterator(final Comparator<T> c, final Iterator<T>[] its) { this.heap = new PriorityQueue<>(11, new TupleKeyComparator<T, Iterator<T>>(c)); for (final Iterator<T> it : its) { final T b = it.hasNext() ? it.next() : null; if (b != null) { heap.add(new Tuple<>(b, it)); } } } MergingIterator(final Comparator<T> c, final Iterator<T>[] its); }
MergingIterator implements Iterator<T> { public MergingIterator(final Comparator<T> c, final Iterator<T>[] its) { this.heap = new PriorityQueue<>(11, new TupleKeyComparator<T, Iterator<T>>(c)); for (final Iterator<T> it : its) { final T b = it.hasNext() ? it.next() : null; if (b != null) { heap.add(new Tuple<>(b, it)); } } } MergingIterator(final Comparator<T> c, final Iterator<T>[] its); @Override boolean hasNext(); @Override T next(); @Override void remove(); }
MergingIterator implements Iterator<T> { public MergingIterator(final Comparator<T> c, final Iterator<T>[] its) { this.heap = new PriorityQueue<>(11, new TupleKeyComparator<T, Iterator<T>>(c)); for (final Iterator<T> it : its) { final T b = it.hasNext() ? it.next() : null; if (b != null) { heap.add(new Tuple<>(b, it)); } } } MergingIterator(final Comparator<T> c, final Iterator<T>[] its); @Override boolean hasNext(); @Override T next(); @Override void remove();  }