Stream API使用
理解
Stream API 关注的是多个数据的计算(排序、查找、过滤、映射、遍历等),面向CPU的。
集合关注的数据的存储,面向内存的。
Stream API 之于集合,类似于SQL之于数据表的查询
注意!
- ①Stream 自己不会存储元素。
- ②Stream 不会改变源对象。相反,他们会返回一个持有结果的新Stream。
- ③Stream 操作是延迟执行的。这意味着他们会等到需要结果的时候才执行。即一旦执行终止操作,就执行中间操作链,并产生结果。
- ④ Stream一旦执行了终止操作,就不能再调用其它中间操作或终止操作了。
Stream 执行流程
- 步骤1:Stream的实例化
- 步骤2:一系列的中间操作
- 步骤3:执行终止操作
创建 Stream三种方式:
/**
* @author Created by xxw on 2023-09-07 16:52
* @Description 测试Stream的实例化
*/
public class StreamAPITest {
//创建 Stream方式一:通过集合
@Test
public void test1(){
List<Employee> list = EmployeeData.getEmployees();
// default Stream<E> stream() : 返回一个顺序流
Stream<Employee> stream = list.stream();
// default Stream<E> parallelStream() : 返回一个并行流
Stream<Employee> stream1 = list.parallelStream();
System.out.println(stream);
System.out.println(stream1);
}
//创建 Stream方式二:通过数组
@Test
public void test2(){
//调用Arrays类的static <T> Stream<T> stream(T[] array): 返回一个流
Integer[] arr = new Integer[]{1,2,3,4,5};
Stream<Integer> stream = Arrays.stream(arr);
int[] arr1 = new int[]{1,2,3,4,5};
IntStream stream1 = Arrays.stream(arr1);
}
//创建 Stream方式三:通过Stream的of()
@Test
public void test3(){
Stream<String> stream = Stream.of("AA", "BB", "CC", "SS", "DD");
}
}
Stream的中间操作
/**
* @author Created by xxw on 2023-09-07 16:53
* @Description Stream的中间操作
*/
public class StreamAPITest1 {
//1-筛选与切片
@Test
public void test1() {
// filter(Predicate p)——接收 Lambda,从流中排除某些元素。
//练习:查询员工表中薪资大于7000的员工信息
List<Employee> list = EmployeeData.getEmployees();
Stream<Employee> stream = list.stream();
stream.filter(emp -> emp.getSalary() > 7000).forEach(System.out::println);
System.out.println();
// limit(n)——截断流,使其元素不超过给定数量。
//错误的。因为stream已经执行了终止操作,就不可以再调用其它的中间操作或终止操作了。
// stream.limit(2).forEach(System.out::println);
list.stream().filter(emp -> emp.getSalary() > 7000).limit(2).forEach(System.out::println);
System.out.println();
// skip(n) —— 跳过元素,返回一个扔掉了前 n 个元素的流。若流中元素不足 n 个,则返回一个空流。与 limit(n) 互补
list.stream().skip(5).forEach(System.out::println);
System.out.println();
// distinct()——筛选,通过流所生成元素的 hashCode() 和 equals() 去除重复元素
list.add(new Employee(1009, "马斯克", 40, 12500.32));
list.add(new Employee(1009, "马斯克", 40, 12500.32));
list.add(new Employee(1009, "马斯克", 40, 12500.32));
list.add(new Employee(1009, "马斯克", 40, 12500.32));
list.stream().distinct().forEach(System.out::println);
}
//2-映射
@Test
public void test2() {
//map(Function f)——接收一个函数作为参数,将元素转换成其他形式或提取信息,该函数会被应用到每个元素上,并将其映射成一个新的元素。
//练习:转换为大写
List<String> list = Arrays.asList("aa", "bb", "cc", "dd");
//方式1:
list.stream().map(str -> str.toUpperCase()).forEach(System.out::println);
//方式2:
list.stream().map(String :: toUpperCase).forEach(System.out::println);
//练习:获取员工姓名长度大于3的员工。
List<Employee> employees = EmployeeData.getEmployees();
employees.stream().filter(emp -> emp.getName().length() > 3).forEach(System.out::println);
//练习:获取员工姓名长度大于3的员工的姓名。
//方式1:
employees.stream().filter(emp -> emp.getName().length() > 3).map(emp -> emp.getName()).forEach(System.out::println);
//方式2:
employees.stream().map(emp -> emp.getName()).filter(name -> name.length() > 3).forEach(System.out::println);
//方式3:
employees.stream().map(Employee::getName).filter(name -> name.length() > 3).forEach(System.out::println);
}
//3-排序
@Test
public void test3() {
//sorted()——自然排序
Integer[] arr = new Integer[]{345,3,64,3,46,7,3,34,65,68};
String[] arr1 = new String[]{"GG","DD","MM","SS","JJ"};
Arrays.stream(arr).sorted().forEach(System.out::println);
System.out.println(Arrays.toString(arr));//arr数组并没有因为升序,做调整。
Arrays.stream(arr1).sorted().forEach(System.out::println);
//因为Employee没有实现Comparable接口,所以报错!
// List<Employee> list = EmployeeData.getEmployees();
// list.stream().sorted().forEach(System.out::println);
//sorted(Comparator com)——定制排序
List<Employee> list = EmployeeData.getEmployees();
list.stream().sorted((e1,e2) -> e1.getAge() - e2.getAge()).forEach(System.out::println);
//针对于字符串从大大小排列
Arrays.stream(arr1).sorted((s1,s2) -> -s1.compareTo(s2)).forEach(System.out::println);
// Arrays.stream(arr1).sorted(String :: compareTo).forEach(System.out::println);
}
}
Stream的终止操作
/**
* @author Created by xxw on 2023-09-07 16:54
* @Description Stream的终止操作
*/
public class StreamAPITest2 {
//1-匹配与查找
@Test
public void test1(){
// allMatch(Predicate p)——检查是否匹配所有元素。
// 练习:是否所有的员工的年龄都大于18
List<Employee> list = EmployeeData.getEmployees();
System.out.println(list.stream().allMatch(emp -> emp.getAge() > 18));
// anyMatch(Predicate p)——检查是否至少匹配一个元素。
//练习:是否存在年龄大于18岁的员工
System.out.println(list.stream().anyMatch(emp -> emp.getAge() > 18));
// 练习:是否存在员工的工资大于 10000
System.out.println(list.stream().anyMatch(emp -> emp.getSalary() > 10000));
// findFirst——返回第一个元素
System.out.println(list.stream().findFirst().get());
}
@Test
public void test2(){
// count——返回流中元素的总个数
List<Employee> list = EmployeeData.getEmployees();
System.out.println(list.stream().filter(emp -> emp.getSalary() > 7000).count());
// max(Comparator c)——返回流中最大值
//练习:返回最高工资的员工
System.out.println(list.stream().max((e1, e2) -> Double.compare(e1.getSalary(), e2.getSalary())));
// 练习:返回最高的工资:
//方式1:
System.out.println(list.stream().max((e1, e2) -> Double.compare(e1.getSalary(), e2.getSalary())).get().getSalary());
//方式2:
System.out.println(list.stream().map(emp -> emp.getSalary()).max((salary1, salary2) -> Double.compare(salary1, salary2)).get());
System.out.println(list.stream().map(emp -> emp.getSalary()).max(Double::compare).get());
// min(Comparator c)——返回流中最小值
// 练习:返回最低工资的员工
System.out.println(list.stream().min((e1, e2) -> Double.compare(e1.getSalary(), e2.getSalary())));
// forEach(Consumer c)——内部迭代
list.stream().forEach(System.out::println);
//针对于集合,jdk8中增加了一个遍历的方法
list.forEach(System.out::println);
//针对于List来说,遍历的方式:① 使用Iterator ② 增强for ③ 一般for ④ forEach()
}
//2-归约
@Test
public void test3(){
// reduce(T identity, BinaryOperator)——可以将流中元素反复结合起来,得到一个值。返回 T
// 练习1:计算1-10的自然数的和
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
System.out.println(list.stream().reduce(0, (x1, x2) -> x1 + x2));
System.out.println(list.stream().reduce(0, (x1, x2) -> Integer.sum(x1,x2)));
System.out.println(list.stream().reduce(0, Integer::sum));
System.out.println(list.stream().reduce(10, (x1, x2) -> x1 + x2));
// reduce(BinaryOperator) ——可以将流中元素反复结合起来,得到一个值。返回 Optional<T>
// 练习2:计算公司所有员工工资的总和
List<Employee> employeeList = EmployeeData.getEmployees();
System.out.println(employeeList.stream().map(emp -> emp.getSalary()).reduce((salary1, salary2) -> Double.sum(salary1, salary2)));
System.out.println(employeeList.stream().map(emp -> emp.getSalary()).reduce(Double::sum));
}
//3-收集
@Test
public void test4(){
List<Employee> list = EmployeeData.getEmployees();
// collect(Collector c)——将流转换为其他形式。接收一个 Collector接口的实现,用于给Stream中元素做汇总的方法
// 练习1:查找工资大于6000的员工,结果返回为一个List或Set
List<Employee> list1 = list.stream().filter(emp -> emp.getSalary() > 6000).collect(Collectors.toList());
list1.forEach(System.out::println);
System.out.println();
list.forEach(System.out::println);
System.out.println();
// 练习2:按照员工的年龄进行排序,返回到一个新的List中
List<Employee> list2 = list.stream().sorted((e1, e2) -> e1.getAge() - e2.getAge()).collect(Collectors.toList());
list2.forEach(System.out::println);
}
}