3.终止操作(终端操作)
(一)查找与匹配
allMatch-检查是否匹配所有元素
anyMatch-检查是否至少匹配一个元素
noneMatch-检查是否没有匹配所有元素
findFirst-返回第一个元素
findAny-返回当前流中的任意元素
count-返回流中元素的总个数
max-返回流中最大值
@Test
public void test(){
boolean b1=employees.stream()
.allMatch((e)->e.getStatus().equals(Status.BUSY));
System.out.println(b1);
boolean b2=employees.stream()
.anyMatch((e)->e.getStatus().equals(Status.BUSY));
System.out.println(b2);
boolean b3=employees.stream()
.noneMatch((e)->e.getStatus().equals(Status.BUSY));
System.out.println(b3);
Optional<Employee> op=employees.stream()
.sorted((e1,e2)->Double.compare(e1.getSalary(), e2.getSalary()))
.findFirst();
System.out.println(op.get());
Optional<Employee> op2=employees.stream()
.filter((e)->e.getStatus().equals(Status.FREE))
.findAny();
System.out.println(op2.get());
}
@Test
public void test2(){
Long count=employees.stream()
.count();
System.out.println(count);
Optional<Employee> op=employees.stream()
.max((e1,e2)->Double.compare(e1.getSalary(), e2.getSalary()));
System.out.println(op.get());
Optional<Double> op2=employees.stream()
.map(Employee::getSalary)
.min(Double::compare);
System.out.println(op2.get());
}
(二)规约
reduce(T identity,BinaryOperator)
reduce(BinaryOperator)
可以将流中元素反复结合起来,得到一个值
@Test
public void test3(){
List<Integer> list=Arrays.asList(1,2,3,4,5,6,7,8,9);
Integer sum=list.stream()
.reduce(0,(x,y)->x+y); //起始值是x,也就是这里的0,流中的元素是y,也就是(1,2...9)
System.out.println(sum);
System.out.println("---------------------");
Optional<Double> op=employees.stream() //一定不为空值用Integer,可能为空的值用Optional
.map(Employee::getSalary)
.reduce(Double::sum);
System.out.println(op.get());
}
(三)收集
collect-将流转换为其他形式,接收一个Collector接口的实现,用于给Stream中元素做汇总的方法
@Test
public void test4(){
//方法一:List
List<String> list=employees.stream()
.map(Employee::getName)
.collect(Collectors.toList()); //collector接口中方法的实现决定了如何对流执行收集操作
list.forEach(System.out::println);
System.out.println("---------------------");
//方法二:Set
Set<String> set=employees.stream()
.map(Employee::getName)
.collect(Collectors.toSet());
set.forEach(System.out::println);
System.out.println("---------------------");
//方法三:HashSet
HashSet<String> hs=employees.stream()
.map(Employee::getName)
.collect(Collectors.toCollection(HashSet::new));
hs.forEach(System.out::println);
}
@Test
public void test5(){
//总数
Long count=employees.stream()
.collect(Collectors.counting());
System.out.println(count);
System.out.println("---------------------");
//平均数
Double avg=employees.stream()
.collect(Collectors.averagingDouble(Employee::getSalary));
System.out.println(avg);
//总和
Double sum=employees.stream()
.collect(Collectors.summingDouble(Employee::getSalary));
System.out.println(sum);
//最大值
Optional<Employee> max=employees.stream()
.collect(Collectors.maxBy((e1,e2)->Double.compare(e1.getSalary(),e2.getSalary())));
System.out.println(max);
//最小值
Optional<Double> min=employees.stream()
.map(Employee::getSalary)
.collect(Collectors.minBy(Double::compare));
System.out.println(min.get());
}
分组
@Test
public void test6(){
Map<Status,List<Employee>> map=employees.stream()
.collect(Collectors.groupingBy(Employee::getStatus));
System.out.println(map);
}
多级分组
@Test
public void test7(){
Map<Status,Map<String,List<Employee>>> map=employees.stream()
.collect(Collectors.groupingBy(Employee::getStatus,Collectors.groupingBy((e)->{
if(((Employee)e).getAge()<35){
return "1";
}else if(((Employee)e).getAge()>50){
return "2";
}else{
return "3";
}
})));
System.out.println(map);
}
分片
@Test
public void test8(){
Map<Boolean,List<Employee>> map=employees.stream()
.collect(Collectors.partitioningBy((e)->e.getSalary()>8000));
System.out.println(map);
}