一、 Stream概述
Java 8 是一个非常成功的版本,这个版本新增的Stream,配合同版本出现的 Lambda ,给我们操作集合(Collection)提供了极大的便利。那么什么是Stream?
Stream将要处理的元素集合看作一种流,在流的过程中,借助Stream API对流中的元素进行操作,比如:筛选、排序、聚合等。
1. Stream API vs 集合框架 > Stream API 关注的是多个数据的计算(排序、查找、过滤、映射、遍历等),面向CPU的。 集合关注的数据的存储,面向内存的。 > Stream API 之于集合,类似于SQL之于数据表的查询。 2. 使用说明 ①Stream 自己不会存储元素。 ②Stream 不会改变源对象。相反,他们会返回一个持有结果的新Stream。 ③Stream 操作是延迟执行的。这意味着他们会等到需要结果的时候才执行。即一旦执行终止操作,就执行中间操作链,并产生结果。 ④ Stream一旦执行了终止操作,就不能再调用其它中间操作或终止操作了。 3. Stream 执行流程 步骤1:Stream的实例化 步骤2:一系列的中间操作 步骤3:执行终止操作
二、创建Stream
1.创建 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); }
2.创建 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); }
3.创建 Stream方式三:通过Stream的of()
@Test public void test3(){ Stream<String> stream = Stream.of("AA", "BB", "CC", "SS", "DD"); }
三、案例实战
案例说明都在代码里啦
1.引入Employee 类
public class Employee { private int id; private String name; private int age; private double salary; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } public Employee() { System.out.println("Employee()....."); } public Employee(int id) { this.id = id; } public Employee(int id, String name) { this.id = id; this.name = name; } public Employee(int id, String name, int age, double salary) { this.id = id; this.name = name; this.age = age; this.salary = salary; } @Override public String toString() { return "Employee{" + "id=" + id + ", name='" + name + '\'' + ", age=" + age + ", salary=" + salary + '}'; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Employee employee = (Employee) o; if (id != employee.id) return false; if (age != employee.age) return false; if (Double.compare(employee.salary, salary) != 0) return false; return name != null ? name.equals(employee.name) : employee.name == null; } @Override public int hashCode() { int result; long temp; result = id; result = 31 * result + (name != null ? name.hashCode() : 0); result = 31 * result + age; temp = Double.doubleToLongBits(salary); result = 31 * result + (int) (temp ^ (temp >>> 32)); return result; } }
2.引入测试数据类
public class EmployeeData { public static List<Employee> getEmployees(){ List<Employee> list = new ArrayList<>(); list.add(new Employee(1001, "马化腾", 34, 6000.38)); list.add(new Employee(1002, "马云", 2, 19876.12)); list.add(new Employee(1003, "刘强东", 33, 3000.82)); list.add(new Employee(1004, "雷军", 26, 7657.37)); list.add(new Employee(1005, "李彦宏", 65, 5555.32)); list.add(new Employee(1006, "比尔盖茨", 42, 9500.43)); list.add(new Employee(1007, "任正非", 26, 4333.32)); list.add(new Employee(1008, "扎克伯格", 35, 2500.32)); return list; } }
3.1 筛选与切片
filter()、limit()、skip()、distinct()
@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); }
3.2 映射 map
map(Function f)——接收一个函数作为参数,将元素转换成其他形式或提取信息,该函数会被应用到每个元素上,并将其映射成一个新的元素。
@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.3-排序 sorted()
@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); } }
3.4 匹配与查找 allMatch()、anyMatch()、findFirst()
@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()); }
count()、max()、min()
@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() }
3.5 归约 reduce()
reduce(T identity, BinaryOperator)——可以将流中元素反复结合起来,得到一个值。返回 T
@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.6 收集 collect()
collect(Collector c)——将流转换为其他形式。接收一个 Collector接口的实现,用于给Stream中元素做汇总的方法
@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); } }