11

In java 8,java.util.stream.Stream#forEach consider as a replacement for traditional for-loop. But why not this is not chain function.Says it return void instead of Stream<T> it self.

Like this

Arrays
    .stream(Girls.toArray())
    .forEach(Girls::getUp)
    .forEach(Girls::dressUp)
    .filter(/* Top 10 Girls */)
    .forEach(Gay.getMe()::gotGirl)
    .endFilter()// Not an API, but it means remove last filter
    .filter(/* Worst 10 Girls */)
    .forEach(Gay.get(0)::gotGirl)


girls = Arrays
    .stream(Girls.toArray());
girls.forEach(g->{g.getUp();g.dressUp()});
girls.filter(/* Top 10 Girls */)
    .forEach(Gay.getMe()::gotGirl);
girls.filter(/* Worst 10 Girls */)
    .forEach(Gay.get(0)::gotGirl);

The first is nice than second one.But the first one got worse performance.

So, why forEach is not chainable ?

6 Answers 6

28

The methods you are looking for exist and are called Stream::peek and Stream::map. With Stream::peek, the above code might look as follows.

Arrays
    .stream(Girls.toArray())
    .peek(Girls::getUp)
    .peek(Girls::dressUp)
    .filter(/* Top 10 Girls */)
    .peek(Gay.getMe()::gotGirl)
    ...
1
  • 12
    However, note that having side-effects in peek() or map() behavioral parameters that may interfere with the source, or affect the result of other stream operations, is a no-no. The peek() method is really there just for debugging/logging, not for computing the result; similarly, map() should be used only for transforming, not for hiding side-effects. Jun 14, 2014 at 15:18
13

Because forEach is a terminal operation. It forces the stream to consume all its elements and call the consumer for each of them. Once that is done, the stream has been consumed, and can't be reused.

A Stream has as many intermediate operations as you want, but can have just one terminal operation.

9

The forEach operation is indeed a terminal operation and is thus not chainable, but it is possible to compose several operations (consumers, in this case) into a single forEach call. For example:

    Consumer<String> c1 = s -> System.out.println(s + "1");
    Consumer<String> c2 = s -> System.out.println(s + "2");
    Consumer<String> c3 = s -> System.out.println(s + "3");
    Arrays.asList("a", "b", "c")
          .stream()
          .forEach(c1.andThen(c2).andThen(c3));

Unfortunately it doesn't appear possible to write the lambdas inline without a bunch of ugly casting.

5

Continuing from Stuart Marks' answer, it is possible to write the lambdas inline but only the first consumer needs to be cast :

Arrays.asList("a", "b", "c").forEach(
    ((Consumer<String>) s -> System.out.println(s + "1"))
    .andThen(s->System.out.println(s + "2"))
    .andThen(s -> System.out.println(s + "3"))
);
1
  • 4
    Every time you do an explicit cast a fairy dies. Jun 11, 2015 at 5:18
3

Chaining with andThen is indeed the way to go, but we need some syntactic sugar to make it pretty:

public class Consumers {
    public static <T> Consumer<T> of(Consumer<T> base) {
        return base;
    }
}

This allows us to do:

Arrays.asList("a", "b", "c")
      .stream()
      .forEach(Consumers.of(s -> System.out.println(s + "1"))
                        .andThen(s -> System.out.println(s + "2"))
                        .andThen(s -> System.out.println(s + "3")));

Or (shorter):

Arrays.asList("a", "b", "c")
      .forEach(Consumers.of(s -> System.out.println(s + "1"))
                        .andThen(s -> System.out.println(s + "2"))
                        .andThen(s -> System.out.println(s + "3")));

(since forEachis available directly on the collection)

2
  • Using forEach in the collection prevents you from doing in a parallel way. Another thing, what is the point of the Consumers class? You can use your Consumer directly instead of passing it to a method and returning without doing nothing. Aug 23, 2022 at 1:14
  • @PhilippeGioseffi Without the helper method you will have to cast s -> System.out.println(s + "1") to Consumer<String>. And as we all know, casting kills kittens :) Aug 24, 2022 at 8:25
0

As andThen is defined in Java 8 as

default Consumer<T> andThen(Consumer<? super T> after) {
    Objects.requireNonNull(after);
    return (T t) -> { accept(t); after.accept(t); };
}

you can easily create your own composite Consumer<T> for any number of your functions

class CompositeConsumer<T> implements Consumer<T> {
    private final List<Consumer<T>> funcs;

    CompositeConsumer(List<Consumer<T>> funcs) {
        this.funcs = funcs;
    }

    @Override
    public void accept(T t) {
        for (Consumer<T> func: funcs) {
            func.accept(t); 
        } 
    }
}

and use it for any number of your functions at once

List<Consumer<String>> funcs = ...
Arrays.asList("a", "b", "c")
      .forEach(new CompositeConsumer(funcs));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.