Collectors 实现了接口 Collector<T,A,R>
T: 需要进行reduce操作的元素类型
A:reduce操作的动态集合类型
R:reduce操作的结果类型
- 举例
//将名字集合到list
List<String> list = people.stream().map(Person::getName).collect(Collectors.toList());
//将名字集合到TreeSet
Set<String> set = people.stream().map(Person::getName).collect(Collectors.toCollection(TreeSet::new));
//将名字转换为String,并连接为一个字符串
String joined = things.stream()
.map(Object::toString)
.collect(Collectors.joining(", "));
//计算员工的工资总额
int total = employees.stream()
.collect(Collectors.summingInt(Employee::getSalary)));
//根据部门将员工分组:
Map<Department, List<Employee>> byDept
= employees.stream()
.collect(Collectors.groupingBy(Employee::getDepartment));
//计算不同部门的员工工资总额
Map<Department, Integer> totalByDept
= employees.stream()
.collect(Collectors.groupingBy(Employee::getDepartment,
Collectors.summingInt(Employee::getSalary)));
//划分及格和不及格的学生
Map<Boolean, List<Student>> passingFailing =
students.stream()
.collect(Collectors.partitioningBy(s -> s.getGrade() >= PASS_THRESHOLD));
Collectors的官方说明见:
官网文档
如果你对Function接口不是很了解,那么在使用这些方法时会很容易出错,下面进行简单的介绍:
Function<T, R>
T—函数的输入类型
R-函数的输出类型
也就是通过这个函数,可以将一个类型转换为另一个类型,比如下面的例子:
//定义一个function 输入是String类型,输出是 EventInfo 类型, EventInfo是一个类。
Function<String, EventInfo> times2 = fun -> { EventInfo a = new EventInfo(); a.setName(fun); return a;};
String[] testintStrings={"1","2","3","4"};
//将String 的Array转换成map,调用times2函数进行转换
Map<String,EventInfo> eventmap1=Stream.of(testintStrings).collect(Collectors.toMap(inputvalue->inputvalue, inputvalue->times2.apply(inputvalue)));
如果Collectors.toMap的转换过程很简单,比如输入和输出类型相同,则不需要另外定义Function,例如:
Map<String,String> eventmap2=Stream.of(testStrings).collect(Collectors.toMap(inputvalue->inputvalue, inputvalue->(inputvalue+”a”)));