学习笔记:Stream流的创建、中间以及终端操作
一、Stream流的创建
惰性操作,每当访问到流中的一个元素,才在此元素上执行相关操作,Stream不保存数据,故每个Stream流只能使用一次,分为中间操作和终止操作,中间操作:返回结果为Stream,可有多个中间操作,终止操作:返回数据,只能有一个。
* Stream的实例化
* 创建方式:
* 方式一:通过集合创建
* 方式二:通过数组
* 方式三:通过Stream的of()、iterate()、generate()
//方式一:通过集合
@Test
public void test1(){
List<Employee> employees = EmployeeData.getEmployees();
//Stream中的静态方法:default Stream<E> stream() 返回一个顺序流
Stream<Employee> stream = employees.stream();
//Stream中的静态方法:default Stream<E> parallelStream() 返回一个并行流
Stream<Employee> employeeStream = employees.parallelStream();
}
//方式二:通过数组
@Test
public void test2(){
int[] arr = new int[]{1,2,3,4,5};
//调用Array类的static <T> Stream<T> stream(T[] array):返回一个流
IntStream stream = Arrays.stream(arr);
System.out.println(stream);
}
//方式三:通过Stream的of()、iterate()、generate()
@Test
public void test3(){
Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5);
Stream<Integer> stream1 = Stream.iterate(0, x -> x + 3).limit(4);
stream1.forEach(t-> System.out.println());
// stream1.forEach(System.out::println);
Stream<Double> stream2 = Stream.generate(Math::random).limit(4);
stream2.forEach(System.out::println);
}
二、中间操作
在这里所用的EmployeeData
数据
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, "马云", 12, 9876.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;
}
}
filter
List<Employee> list = EmployeeData.getEmployees();
//filter(Predicate p)--接收Lambda,从流中排除某些元素
Stream<Employee> stream = list.stream();
//查询员工表中薪资大于8000的员工信息
stream.filter(e -> e.getSalary() > 8000).forEach(System.out::println);
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
limit
//limit(n) 截断流,使元素不超过给定数量
//Stream<Employee> stream2 = stream.limit(3); 此处错误,应该重新创建list的流,每个Stream流只能使用一次,否则会出现错误: java.lang.IllegalStateException: stream has already been operated upon or closed
Stream<Employee> stream1 = list.stream();
stream1.limit(3).forEach(System.out::println);
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
limit
//skip(n) 跳过前n个元素
Stream<Employee> stream2 = list.stream();
stream2.skip(3).forEach(System.out::println);
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
distinct
//distinct() 筛选,通过流所生成元素的hashCode() 和equals()去除重复元素
List<Integer> list1 = Arrays.asList(1, 2, 2, 2, 5, 6, 7);
list1.stream().distinct().forEach(System.out::println);
排序
@Test
public void test2() {
//sorted() 自然排序
List<Integer> list = Arrays.asList(1, 2, 3, 5, 6, -9, 0, 8, -99);
String[] strArr = { "g","f","bc", "dee", "f" };
list.stream().sorted().forEach(System.out::println);
System.out.println("-----------");
Arrays.stream(strArr).sorted().forEach(System.out::println);
System.out.println("-----------");
//sorted(Comparator com) 定制排序
List<Employee> employees = EmployeeData.getEmployees();
//按照salary排序
//employees.stream().sorted(Comparator.comparingDouble(Employee::getSalary)).forEach(System.out::println);
employees.stream().sorted((e1, e2) -> Double.compare(e1.getSalary(), e2.getSalary())).forEach(System.out::println);
//按照age进行排序
System.out.println("-----------");
employees.stream().sorted((e1, e2) -> Integer.compare(e1.getAge(), e2.getAge())).forEach(System.out::println);
}
三、终止操作
forEach
List<Integer> list1 = Arrays.asList(1, 2, 3, 4);
list1.forEach(System.out::println);
匹配与查找
@Test
public void test1() {
//allMatch(Predicate p) 检查是否匹配所有元素
List<Employee> employees = EmployeeData.getEmployees();
boolean allMatch = employees.stream().allMatch(e -> e.getAge() > 18);
//anyMatch(Predicate p) 检查是否至少匹配一个元素
boolean anyMatch = employees.stream().anyMatch(e -> e.getSalary() > 1000);
//noneMatch(Predicate p) 检查是否没有匹配所有元素(全部不满足则true,否则false)
//检查是否有“马”开头的姓名,有则返回false 无则返回true
boolean noneMatch = employees.stream().noneMatch(e -> e.getName().startsWith("马"));
//findFirst() 返回第一个元素
Optional<Employee> employee = employees.stream().findFirst();
System.out.println(employee);
//findAny() 返回当前流中的任意元素
Optional<Employee> any = employees.stream().findAny();
System.out.println(any);
//count() 返回流中元素总数
long count = employees.stream().filter(e -> e.getSalary() > 5000).count();
System.out.println(count);
//max(Comparator c) 返回流中最大值
//获取工资最高的对象
//Optional<Employee> max = list.stream().max(Comparator.comparingDouble(Employee::getSalary));
//获取最高的工资
Stream<Double> salaryStream = employees.stream().map(e -> e.getSalary()); //获取员工工资
Optional<Double> max = salaryStream.max(Double::compare); //得到最大的工资
System.out.println(max);
//min(Comparator c) 返回流中最小值
//返回最低工资的员工
Optional<Employee> employee1 = employees.stream().min((e1, e2) -> Double.compare(e1.getSalary(), e2.getSalary()));
System.out.println(employee1);
//forEach(Consumer c) 内部迭代
employees.stream().forEach(System.out::println);
}
归约
@Test
public void test2() {
//reduce(T iden, BinaryOperator b) 可以将流中元素反复结合起来,得到一个值。返回 T
//计算1-10的自然数的和
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
Integer sum = list.stream().reduce(0, Integer::sum);//方法引用 类::静态方法
System.out.println(sum);
//reduce(BinaryOperator b) 可以将流中元素反复结合起来,得到一个值。返回 Optional<T>
//计算所有员工工资的总和
List<Employee> employees = EmployeeData.getEmployees();
Stream<Double> salaryStraem = employees.stream().map(Employee::getSalary); //获取工资流
//Optional<Double> sum1 = salaryStraem.reduce(Double::sum);
Optional<Double> sum1 = salaryStraem.reduce((s1, s2) -> s1 + s2);
System.out.println(sum1);
}
收集
@Test
public void test3() {
//collect(Collector c)将流转换为其他形式。接收一个 Collector接口的实现,用于给Stream中元素做汇总的方法
//查找工资大于8000的员工,结果返回一个List或Set
List<Employee> employees = EmployeeData.getEmployees();
List<Employee> collect = employees.stream().filter(employee -> employee.getSalary() > 8000).collect(Collectors.toList());
collect.forEach(System.out::println);
Set<Employee> collect1 = employees.stream().filter(employee -> employee.getSalary() > 8000).collect(Collectors.toSet());
collect1.forEach(System.out::println);
}