Java Collectors partitioningBy()方法
Collectors partitioningBy() 方法是java.util.stream.Collectors类的一个预定义方法,用于根据一个给定的谓词对一个对象流(或一个元素集)进行分割。该方法有两个重载的变体。其中一个只接受一个谓词作为参数,而另一个则接受谓词和收集器实例作为参数。
partitioningBy(Predicate<? super T> predicate)
语法
public static <T> Collector<T, ?, Map<Boolean, List<T>>> partitioningBy(Predicate<? super T> predicate)
其中。
- **接口 Collector < **T, A, R > : 一个可变的还原操作,将输入元素累积到一个可变的结果容器中,在所有输入元素被处理后,可选择将累积的结果转化为最终表示。还原操作可以按顺序进行,也可以并行进行。
- T: 还原操作的输入元素的类型。
 - A: 还原操作的可变累积类型。
 - R: 还原操作的结果类型。
 
 - **Map <Boolean, List
>: **包含输出的地图。键是布尔值(真或假),相应的值是包含T类型元素的列表。  
参数: 该方法需要一个强制性的参数predicate,它是一个T类型的Predicate接口的实例。
返回值: 该方法返回一个实现分区操作的 收集器 。
下面是一个例子来说明partitioningBy()方法。
程序。
// Java code to show the implementation of
// Collectors partitioningBy() function
  
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
  
class Gfg {
  
    // Driver code
    public static void main(String[] args)
    {
        // creating an Integer stream
        Stream<Integer>
            s = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
  
        // using Collectors partitioningBy()
        // method to split the stream of elements into
        // 2 parts, greater than 3 and less than 3.
        Map<Boolean, List<Integer> >
            map = s.collect(
                Collectors.partitioningBy(num -> num > 3));
  
        // Displaying the result as a map
        // true if greater than 3, false otherwise
        System.out.println("Elements in stream "
                           + "partitioned by "
                           + "less than equal to 3: \n"
                           + map);
    }
}
输出:
Elements in stream partitioned by less than equal to 3: 
{false=[1, 2, 3], true=[4, 5, 6, 7, 8, 9, 10]}
partitioningBy(Predicate<? super T>predicate, Collector<? super T, A, D> downstream)
语法
public static <T> Collector<T, ?, Map<Boolean, List<T>>> partitioningBy(Predicate<? super T>predicate, Collector<? super T, A, D> downstream)
其中。
- **接口 Collector < **T, A, R > : 一个可变的还原操作,将输入元素累积到一个可变的结果容器中,在所有输入元素被处理后,可选择将累积的结果转化为最终表示。还原操作可以按顺序进行,也可以并行进行。
- T: 还原操作的输入元素的类型。
 - A: 还原操作的可变累积类型。
 - R: 还原操作的结果类型。
 
 - **Map <Boolean, List
>: **包含输出的地图。键是布尔值(真或假),相应的值是包含T类型元素的列表。  
参数: 该方法需要两个参数,一个是Predicate接口的实例,类型为T,另一个是收集器,用于实现 “下游还原 “并产生输出:
返回值: 该方法返回一个实现分区操作的 收集器 。
下面是一个例子来说明partitioningBy()方法的类型2。
// Java code to show the implementation of
// Collectors partitioningBy() function
  
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
  
class ArraytoArrayList {
  
    // Driver code
    public static void main(String[] args)
    {
        // creating an Integer stream
        Stream<Integer>
            s = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
  
        // Using Collectors.counting() method
        // to count the number of elements in
        // the 2 partitions
        Map<Boolean, Long>
            map = s.collect(
                Collectors.partitioningBy(
                    num -> (num > 3), Collectors.counting()));
  
        // Displaying the result as a map
        // true if greater than 3, false otherwise
        System.out.println("Elements in stream "
                           + "partitioned by "
                           + "less than equal to 3: \n"
                           + map);
    }
}
输出:
Elements in stream partitioned by less than equal to 3: 
{false=3, true=7}
极客教程