IteratorUtils { public static <S> S reduce(final Iterator<S> iterator, final S identity, final BinaryOperator<S> accumulator) { S result = identity; while (iterator.hasNext()) { result = accumulator.apply(result, iterator.next()); } return result; } 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 shouldReduceFromIteratorWithBinaryOperator() { final List<String> iterable = new ArrayList<>(); iterable.add("1"); iterable.add("2"); iterable.add("3"); assertEquals("test123", IteratorUtils.reduce(iterable.iterator(), "test", (a, b) -> a + b)); }
@Test public void shouldReduceFromIterableWithBinaryOperator() { final List<String> iterable = new ArrayList<>(); iterable.add("1"); iterable.add("2"); iterable.add("3"); assertEquals("test123", IteratorUtils.reduce(iterable, "test", (a, b) -> a + b)); }
@Test public void shouldReduceFromIterator() { final List<String> iterable = new ArrayList<>(); iterable.add("1"); iterable.add("2"); iterable.add("3"); assertEquals(new Integer(16), IteratorUtils.reduce(iterable.iterator(), 10, (accumulator, val) -> accumulator + Integer.parseInt(val))); }
@Test public void shouldReduceFromIterable() { final List<String> iterable = new ArrayList<>(); iterable.add("1"); iterable.add("2"); iterable.add("3"); assertEquals(new Integer(16), IteratorUtils.reduce(iterable, 10, (accumulator, val) -> accumulator + Integer.parseInt(val))); }