1.steam总结
2.先列出所有的例子
Stream#of()
五、StreamAPI
1. Stream API概述
创建stream
1。stream()
2.**通过数组**
- `public static IntStream stream(int[] array)`
-
public static LongStream stream(long[] array) -
public static DoubleStream stream(double[] array) -
3.通过Stream的of()方法
3.中间操作
除非流水线上触发终止操作,否则中间操作不会执行任何的处理!而在终止操作时一次性全部处理,称为**惰性求值**。
3.2.1 筛选与切片
3.2.2 映射(不重要)
3.2.3 排序
3.3 步骤三 终止操作(不是很常用)
终端操作会从流的流水线生成结果。其结果可以是任何不是流的值,例如:List、 Integer,甚至是 void
3.3.3 收集(重要)
Collector 接口中方法的实现决定了如何对流执行收集的操作(如收集到 List、Set、Map)
Collectors 实用类提供了很多静态方法,可以方便地创建常见收集器实例具体方法与实例如下表:
3-收集
``@Test
public void test4(){
//collect(Collector c)——将流转换为其他形式。接收一个 Collector接口的实现,用于给Stream中元素做汇总的方法
//练习1:查找工资大于6000的员工,结果返回为一个List或Set
List employees = EmployeeData.getEmployees();
List employeeList = employees.stream().filter(e -> e.getSalary() > 6000).collect(Collectors.toList());
employeeList.forEach(System.out::println);
System.out.println();
Set<Employee> employeeSet = employees.stream().filter(e -> e.getSalary() > 6000).collect(Collectors.toSet());
employeeSet.forEach(System.out::println);
} ``