TransformTranslator { @VisibleForTesting static <T> Iterator<T> flatten(final Iterator<Iterator<T>> toFlatten) { return new AbstractIterator<T>() { @Nullable Iterator<T> current = null; @Override protected T computeNext() { while (true) { if (current == null) { if (toFlatten.hasNext()) { current = toFlatten.next(); } else { return endOfData(); } } if (current.hasNext()) { return current.next(); } current = null; } } }; } private  TransformTranslator();   }
@Test public void testIteratorFlatten() { List<Integer> first = Arrays.asList(1, 2, 3); List<Integer> second = Arrays.asList(4, 5, 6); List<Integer> result = new ArrayList<>(); Iterators.addAll( result, TransformTranslator.flatten(Arrays.asList(first.iterator(), second.iterator()).iterator())); assertEquals(Arrays.asList(1, 2, 3, 4, 5, 6), result); }