Java 8 peek vs map

前端 未结 1 1067
清酒与你
清酒与你 2020-12-08 02:43

I have following case: there is a list of objects - ProductData which contains several fields:

public class ProductData
{
....
  private String name;
  priva         


        
相关标签:
1条回答
  • 2020-12-08 02:49

    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.

    0 讨论(0)
提交回复
热议问题