IteratorUtils { public static final <S extends Collection<T>, T> S fill(final Iterator<T> iterator, final S collection) { while (iterator.hasNext()) { collection.add(iterator.next()); } return collection; } private  IteratorUtils(); static final Iterator<S> of(final S a); static final Iterator<S> of(final S a, S b); static final S fill(final Iterator<T> iterator, final S collection); static void iterate(final Iterator iterator); static final long count(final Iterator iterator); static final long count(final Iterable iterable); static List<S> list(final Iterator<S> iterator); static List<S> list(final Iterator<S> iterator, final Comparator comparator); static Set<S> set(final Iterator<S> iterator); static Iterator<S> limit(final Iterator<S> iterator, final int limit); static boolean allMatch(final Iterator<T> iterator, final Predicate<T> predicate); static boolean anyMatch(final Iterator<T> iterator, final Predicate<T> predicate); static boolean noneMatch(final Iterator<T> iterator, final Predicate<T> predicate); static Map<K, S> collectMap(final Iterator<S> iterator, final Function<S, K> key); static Map<K, V> collectMap(final Iterator<S> iterator, final Function<S, K> key, final Function<S, V> value); static Map<K, List<S>> groupBy(final Iterator<S> iterator, final Function<S, K> groupBy); static S reduce(final Iterator<S> iterator, final S identity, final BinaryOperator<S> accumulator); static S reduce(final Iterable<S> iterable, final S identity, final BinaryOperator<S> accumulator); static E reduce(final Iterator<S> iterator, final E identity, final BiFunction<E, S, E> accumulator); static E reduce(final Iterable<S> iterable, final E identity, final BiFunction<E, S, E> accumulator); static final Iterator<S> consume(final Iterator<S> iterator, final Consumer<S> consumer); static final Iterable<S> consume(final Iterable<S> iterable, final Consumer<S> consumer); static final Iterator<E> map(final Iterator<S> iterator, final Function<S, E> function); static final Iterable<E> map(final Iterable<S> iterable, final Function<S, E> function); static final Iterator<S> filter(final Iterator<S> iterator, final Predicate<S> predicate); static final Iterable<S> filter(final Iterable<S> iterable, final Predicate<S> predicate); static final Iterator<E> flatMap(final Iterator<S> iterator, final Function<S, Iterator<E>> function); static final Iterator<S> concat(final Iterator<S>... iterators); static Iterator asIterator(final Object o); static List asList(final Object o); static Stream<T> stream(final Iterator<T> iterator); static Stream<T> stream(final Iterable<T> iterable); static Iterator<T> noRemove(final Iterator<T> iterator); static Iterator<T> removeOnNext(final Iterator<T> iterator);  }
@Test public void shouldFillFromIterator() { final List<String> iterable = new ArrayList<>(); iterable.add("test1"); iterable.add("test2"); iterable.add("test3"); final List<String> newList = new ArrayList<>(); IteratorUtils.fill(iterable.iterator(), newList); assertIterator(newList.iterator(), iterable.size()); }