Java流 – Java流分区
分区是分组的一种特殊情况。
分组数据基于从函数返回的键。可能有很多组。
分区仅处理基于谓词的两个组。评估为true的值为一个组,false为另一个组。
partitioningBy()方法,它收集映射中的数据,其键的总是布尔类型,在两个版本中重载。
partitioningBy(Predicate<? super T> predicate) partitioningBy(Predicate<? super T> predicate, Collector<? super T,A,D> downstream)
例子
第一个版本的partitioningBy()方法返回基于谓词执行分区的收集器。
以下代码显示如何根据性别对employee进行分区。
import java.time.LocalDate; import java.time.Month; import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.stream.Collectors; public class Main { public static void main(String[] args) { Map<Boolean, List<Employee>> partionedByMaleGender = Employee.persons() .stream() .collect(Collectors.partitioningBy(Employee::isMale)); System.out.println(partionedByMaleGender); } } class Employee { public static enum Gender { MALE, FEMALE } private long id; private String name; private Gender gender; private LocalDate dob; private double income; public Employee(long id, String name, Gender gender, LocalDate dob, double income) { this.id = id; this.name = name; this.gender = gender; this.dob = dob; this.income = income; } public boolean isMale() { return this.gender == Gender.MALE; } public static List<Employee> persons() { Employee p1 = new Employee(1, "Jake", Gender.MALE, LocalDate.of(1971, Month.JANUARY, 1), 2343.0); Employee p2 = new Employee(2, "Jack", Gender.MALE, LocalDate.of(1972, Month.JULY, 21), 7100.0); Employee p3 = new Employee(3, "Jane", Gender.FEMALE, LocalDate.of(1973, Month.MAY, 29), 5455.0); Employee p4 = new Employee(4, "Jode", Gender.MALE, LocalDate.of(1974, Month.OCTOBER, 16), 1800.0); Employee p5 = new Employee(5, "Jeny", Gender.FEMALE, LocalDate.of(1975, Month.DECEMBER, 13), 1234.0); Employee p6 = new Employee(6, "Jason", Gender.MALE, LocalDate.of(1976, Month.JUNE, 9), 3211.0); List<Employee> persons = Arrays.asList(p1, p2, p3, p4, p5, p6); return persons; } @Override public String toString() { String str = String.format("(%s, %s, %s, %s, %.2f)\n", id, name, gender, dob, income); return str; } }
上面的代码生成以下结果。
例2
第二个版本允许我们指定另一个可以执行还原操作的收集器。
以下代码显示如何按性别分区员工,并以逗号分隔的字符串收集其名称:
import java.time.LocalDate; import java.time.Month; import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.stream.Collectors; public class Main { public static void main(String[] args) { Map<Boolean,String> partionedByMaleGender = Employee.persons() .stream() .collect(Collectors.partitioningBy(Employee::isMale, Collectors.mapping(Employee::getName, Collectors.joining(", ")))); System.out.println(partionedByMaleGender); } } class Employee { public static enum Gender { MALE, FEMALE } private long id; private String name; private Gender gender; private LocalDate dob; private double income; public Employee(long id, String name, Gender gender, LocalDate dob, double income) { this.id = id; this.name = name; this.gender = gender; this.dob = dob; this.income = income; } public String getName() { return name; } public boolean isMale() { return this.gender == Gender.MALE; } public static List<Employee> persons() { Employee p1 = new Employee(1, "Jake", Gender.MALE, LocalDate.of(1971, Month.JANUARY, 1), 2343.0); Employee p2 = new Employee(2, "Jack", Gender.MALE, LocalDate.of(1972, Month.JULY, 21), 7100.0); Employee p3 = new Employee(3, "Jane", Gender.FEMALE, LocalDate.of(1973, Month.MAY, 29), 5455.0); Employee p4 = new Employee(4, "Jode", Gender.MALE, LocalDate.of(1974, Month.OCTOBER, 16), 1800.0); Employee p5 = new Employee(5, "Jeny", Gender.FEMALE, LocalDate.of(1975, Month.DECEMBER, 13), 1234.0); Employee p6 = new Employee(6, "Jason", Gender.MALE, LocalDate.of(1976, Month.JUNE, 9), 3211.0); List<Employee> persons = Arrays.asList(p1, p2, p3, p4, p5, p6); return persons; } }
上面的代码生成以下结果。