What's the purpose of partitioningBy

前端 未结 5 593
粉色の甜心
粉色の甜心 2020-12-03 04:20

For example, if I intend to partition some elements, I could do something like:

Stream.of(\"I\", \"Love\", \"Stack Overflow\")
      .collect(Collectors.part         


        
5条回答
  •  半阙折子戏
    2020-12-03 05:02

    Another difference between groupingBy and partitioningBy is that the former takes a Function and the latter a Predicate.

    When you pass a method reference or a lambda expression, such as s -> s.length() > 3, they can be used by either of these two methods (the compiler will infer the functional interface type based on the type required by the method you choose).

    However, if you have a Predicate instance, you can only pass it to Collectors.partitioningBy(). It won't be accepted by Collectors.groupingBy().

    And similarly, if you have a Function instance, you can only pass it to Collectors.groupingBy(). It won't be accepted by Collectors.partitioningBy().

提交回复
热议问题