Java 8 peek vs map

There is a reason peek is documented to be mainly for debugging purposes.

Something that ends up being processed inside peek might not be eligible for the terminal operation at all and streams are executed only by a terminal operation.

Suppose a trivial example first:

    List<Integer> list = new ArrayList<>();
    List<Integer> result = Stream.of(1, 2, 3, 4)
            .peek(x -> list.add(x))
            .map(x -> x * 2)
            .collect(Collectors.toList());

    System.out.println(list);
    System.out.println(result);

Everything looks fine right? Because peek will run for all elements in this case. But what happens when you add a filter (and forget about what peek did):

 .peek(x -> list.add(x))
 .map(x -> x * 2)
 .filter(x -> x > 8) // you have inserted a filter here

You are executing peek for every element, but collecting none. You sure you want that?

This can get even trickier:

    long howMany = Stream.of(1, 2, 3, 4)
            .peek(x -> list.add(x))
            .count();

    System.out.println(list);
    System.out.println(howMany);

In java-8 the list is populated, but in jdk-9 peek is not called at all. Since you are not using filter or flatmap you are not modifying the size of the Stream and count only needs it’s size; thus peek is not called at all. Thus relying on peek is a very bad strategy.

Leave a Comment