java快速记忆笔记

80 阅读1分钟

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 筛选与切片
b3bc20ab68ad4259aac42e24ca2e7c7f~tplv-k3u1fbpfcp-zoom-in-crop-mark_1512_0_0_0.webp 3.2.2 映射(不重要)

5df327a7542141cfa5e74a9e4da7c6db~tplv-k3u1fbpfcp-zoom-in-crop-mark_1512_0_0_0.webp 3.2.3 排序

d1b28b2fa753416eb295386fbe52e4ff~tplv-k3u1fbpfcp-zoom-in-crop-mark_1512_0_0_0.webp

3.3 步骤三 终止操作(不是很常用)

终端操作会从流的流水线生成结果。其结果可以是任何不是流的值,例如:ListInteger,甚至是 void

294f56a3a388432aa26d11e3bf28bb7e~tplv-k3u1fbpfcp-zoom-in-crop-mark_1512_0_0_0.webp

ea9469a0391345688130e0e110ccd3f4~tplv-k3u1fbpfcp-zoom-in-crop-mark_1512_0_0_0.webp

3.3.3 收集(重要)

b90ccac4359c41e89c26ecded974a97c~tplv-k3u1fbpfcp-zoom-in-crop-mark_1512_0_0_0.webp Collector 接口中方法的实现决定了如何对流执行收集的操作(如收集到 ListSetMap

Collectors 实用类提供了很多静态方法,可以方便地创建常见收集器实例具体方法与实例如下表:

cad00ac44bc6419095ce1f9cbd586eea~tplv-k3u1fbpfcp-zoom-in-crop-mark_1512_0_0_0.webp

9046417d371c499b8988341cc31a9994~tplv-k3u1fbpfcp-zoom-in-crop-mark_1512_0_0_0.webp 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);

} ``