一、概述

Stream 是 Java8 中处理集合的关键抽象概念,它可以指定你希望对集合进行的操作,可以执行非常复杂的查找、过滤和映射数据等操作。使用Stream API 对集合数据进行操作,就类似于使用 SQL 执行的数据库查询。也可以使用 Stream API 来并行执行操作。
简而言之,Stream API 提供了一种高效且易于使用的处理数据的方式。
特点:

  1. 不是数据结构,不会保存数据。
  2. 不会修改原来的数据源,它会将操作后的数据保存到另外一个对象中。(保留意见:毕竟peek方法可以修改流中元素)
  3. 惰性求值,流在中间处理过程中,只是对操作进行了记录,并不会立即执行,需要等到执行终止操作的时候才会进行实际的计算。

    二、分类

    image.png
  • 无状态: 指元素的处理不受之前元素的影响;
  • 有状态: 指该操作只有拿到所有元素之后才能继续下去。
  • 非短路操作: 指必须处理所有元素才能得到最终结果;
  • 短路操作: 指遇到某些符合条件的元素就可以得到最终结果,如 A || B,只要A为true,则无需判断B的结果。

    三、具体用法

    1. 流的常用创建方法

    1.1 使用Collection下的 stream() 和 parallelStream() 方法
    1. List<String> list = new ArrayList<>();
    2. Stream<String> stream = list.stream(); //获取一个顺序流
    3. Stream<String> parallelStream = list.parallelStream(); //获取一个并行流

1.2 使用Arrays 中的stream()方法,将数组转成流

  1. Integer[] nums = new Integer[10];
  2. Stream<Integer> stream = Arrays.stream(nums);

1.3 使用Stream中的静态方法:of()、iterate()、generate()

  1. Stream<Integer> stream = Stream.of(1,2,3,4,5,6);
  2. Stream<Integer> stream2 = Stream.iterate(0, (x) -> x + 2).limit(6);
  3. stream2.forEach(System.out::println); // 0 2 4 6 8 10
  4. Stream<Double> stream3 = Stream.generate(Math::random).limit(2);
  5. stream3.forEach(System.out::println);

1.4 使用 BufferedReader.lines() 方法,将每行内容转成流

  1. BufferedReader reader = new BufferedReader(new FileReader("F:\\test_stream.txt"));
  2. Stream<String> lineStream = reader.lines();
  3. lineStream.forEach(System.out::println);

1.5 使用 Pattern.splitAsStream() 方法,将字符串分隔成流

  1. Pattern pattern = Pattern.compile(",");
  2. Stream<String> stringStream = pattern.splitAsStream("a,b,c,d");
  3. stringStream.forEach(System.out::println);

2. 流的中间操作

2.1 筛选与切片

  • filter:过滤流中的某些元素
  • limit(n):获取n个元素
  • skip(n):跳过n元素,配合limit(n)可实现分页
  • distinct:通过流中元素的 hashCode() 和 equals() 去除重复元素 ```java Stream stream = Stream.of(6, 4, 6, 7, 3, 9, 8, 10, 12, 14, 14);

Stream newStream = stream.filter(s -> s > 5) //6 6 7 9 8 10 12 14 14 .distinct() //6 7 9 8 10 12 14 .skip(2) //9 8 10 12 14 .limit(2); //9 8 newStream.forEach(System.out::println);

  1. **2.2 映射**
  2. - **map:** 接收一个函数作为参数,该函数会被应用到每个元素上,并将其映射成一个新的元素。
  3. - **flatMap:** 接收一个函数作为参数,将流中的每个值都换成另一个流,然后把所有流连接成一个流。
  4. ```java
  5. List<String> list = Arrays.asList("a,b,c", "1,2,3");
  6. //将每个元素转成一个新的且不带逗号的元素
  7. Stream<String> s1 = list.stream().map(s -> s.replaceAll(",", ""));
  8. s1.forEach(System.out::println); // abc 123
  9. Stream<String> s3 = list.stream().flatMap(s -> {
  10. //将每个元素转换成一个stream
  11. String[] split = s.split(",");
  12. Stream<String> s2 = Arrays.stream(split);
  13. return s2;
  14. });
  15. s3.forEach(System.out::println); // a b c 1 2 3

2.3 排序

  • sorted():自然排序,流中元素需实现Comparable接口
  • sorted(Comparator com):定制排序,自定义Comparator排序器 ```java List list = Arrays.asList(“aa”, “ff”, “dd”); //String 类自身已实现Compareable接口 list.stream().sorted().forEach(System.out::println);// aa dd ff

Student s1 = new Student(“aa”, 10); Student s2 = new Student(“bb”, 20); Student s3 = new Student(“aa”, 30); Student s4 = new Student(“dd”, 40); List studentList = Arrays.asList(s1, s2, s3, s4);

//自定义排序:先按姓名升序,姓名相同则按年龄升序 studentList.stream().sorted( (o1, o2) -> { if (o1.getName().equals(o2.getName())) { return o1.getAge() - o2.getAge(); } else { return o1.getName().compareTo(o2.getName()); } } ).forEach(System.out::println);

  1. **2.4 消费**
  2. - peek:如同于map,能得到流中的每一个元素。但map接收的是一个Function表达式,有返回值;而peek接收的是Consumer表达式,没有返回值。
  3. ```java
  4. Student s1 = new Student("aa", 10);
  5. Student s2 = new Student("bb", 20);
  6. List<Student> studentList = Arrays.asList(s1, s2);
  7. studentList.stream()
  8. .peek(o -> o.setAge(100))
  9. .forEach(System.out::println);
  10. //结果:
  11. Student{name='aa', age=100}
  12. Student{name='bb', age=100}

3. 流的终止操作

3.1 匹配、聚合操作

  • allMatch:接收一个 Predicate 函数,当流中每个元素都符合该断言时才返回true,否则返回false
  • noneMatch:接收一个 Predicate 函数,当流中每个元素都不符合该断言时才返回true,否则返回false
  • anyMatch:接收一个 Predicate 函数,只要流中有一个元素满足该断言则返回true,否则返回false
  • findFirst:返回流中第一个元素
  • findAny:返回流中的任意元素
  • count:返回流中元素的总个数
  • max:返回流中元素最大值
  • min:返回流中元素最小值 ```java List list = Arrays.asList(1, 2, 3, 4, 5);

boolean allMatch = list.stream().allMatch(e -> e > 10); //false boolean noneMatch = list.stream().noneMatch(e -> e > 10); //true boolean anyMatch = list.stream().anyMatch(e -> e > 4); //true

Integer findFirst = list.stream().findFirst().get(); //1 Integer findAny = list.stream().findAny().get(); //1

long count = list.stream().count(); //5 Integer max = list.stream().max(Integer::compareTo).get(); //5 Integer min = list.stream().min(Integer::compareTo).get(); //1

  1. **3.2 规约操作**
  2. - Optional<T> reduce(BinaryOperator<T> accumulator):第一次执行时,accumulator函数的第一个参数为流中的第一个元素,第二个参数为流中元素的第二个元素;第二次执行时,第一个参数为第一次函数执行的结果,第二个参数为流中的第三个元素;依次类推。
  3. - T reduce(T identity, BinaryOperator<T> accumulator):流程跟上面一样,只是第一次执行时,accumulator函数的第一个参数为identity,而第二个参数为流中的第一个元素。
  4. - <U> U reduce(U identity,BiFunction<U, ? super T, U> accumulator,BinaryOperator<U> combiner):在串行流(stream)中,该方法跟第二个方法一样,即第三个参数combiner不会起作用。在并行流(parallelStream)中,我们知道流被fork join出多个线程进行执行,此时每个线程的执行流程就跟第二个方法reduce(identity,accumulator)一样,而第三个参数combiner函数,则是将每个线程的执行结果当成一个新的流,然后使用第一个方法reduce(accumulator)流程进行规约。
  5. ```java
  6. //经过测试,当元素个数小于24时,并行时线程数等于元素个数,当大于等于24时,并行时线程数为16
  7. List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24);
  8. Integer v = list.stream().reduce((x1, x2) -> x1 + x2).get();
  9. System.out.println(v); // 300
  10. Integer v1 = list.stream().reduce(10, (x1, x2) -> x1 + x2);
  11. System.out.println(v1); //310
  12. Integer v2 = list.stream().reduce(0,
  13. (x1, x2) -> {
  14. System.out.println("stream accumulator: x1:" + x1 + " x2:" + x2);
  15. return x1 - x2;
  16. },
  17. (x1, x2) -> {
  18. System.out.println("stream combiner: x1:" + x1 + " x2:" + x2);
  19. return x1 * x2;
  20. });
  21. System.out.println(v2); // -300
  22. Integer v3 = list.parallelStream().reduce(0,
  23. (x1, x2) -> {
  24. System.out.println("parallelStream accumulator: x1:" + x1 + " x2:" + x2);
  25. return x1 - x2;
  26. },
  27. (x1, x2) -> {
  28. System.out.println("parallelStream combiner: x1:" + x1 + " x2:" + x2);
  29. return x1 * x2;
  30. });
  31. System.out.println(v3); //197474048

3.3 收集操作
collect:接收一个Collector实例,将流中元素收集成另外一个数据结构
Collector 是一个接口,有以下5个抽象方法:

3.3.1 Collector 工具库:Collectors

  1. Student s1 = new Student("aa", 10,1);
  2. Student s2 = new Student("bb", 20,2);
  3. Student s3 = new Student("cc", 10,3);
  4. List<Student> list = Arrays.asList(s1, s2, s3);
  5. //装成list
  6. List<Integer> ageList = list.stream().map(Student::getAge).collect(Collectors.toList()); // [10, 20, 10]
  7. //转成set
  8. Set<Integer> ageSet = list.stream().map(Student::getAge).collect(Collectors.toSet()); // [20, 10]
  9. //转成map,注:key不能相同,否则报错
  10. Map<String, Integer> studentMap = list.stream().collect(Collectors.toMap(Student::getName, Student::getAge)); // {cc=10, bb=20, aa=10}
  11. //字符串分隔符连接
  12. String joinName = list.stream().map(Student::getName).collect(Collectors.joining(",", "(", ")")); // (aa,bb,cc)
  13. //聚合操作
  14. //1.学生总数
  15. Long count = list.stream().collect(Collectors.counting()); // 3
  16. //2.最大年龄 (最小的minBy同理)
  17. Integer maxAge = list.stream().map(Student::getAge).collect(Collectors.maxBy(Integer::compare)).get(); // 20
  18. //3.所有人的年龄
  19. Integer sumAge = list.stream().collect(Collectors.summingInt(Student::getAge)); // 40
  20. //4.平均年龄
  21. Double averageAge = list.stream().collect(Collectors.averagingDouble(Student::getAge)); // 13.333333333333334
  22. // 带上以上所有方法
  23. DoubleSummaryStatistics statistics = list.stream().collect(Collectors.summarizingDouble(Student::getAge));
  24. System.out.println("count:" + statistics.getCount() + ",max:" + statistics.getMax() + ",sum:" + statistics.getSum() + ",average:" + statistics.getAverage());
  25. //分组
  26. Map<Integer, List<Student>> ageMap = list.stream().collect(Collectors.groupingBy(Student::getAge));
  27. //多重分组,先根据类型分再根据年龄分
  28. Map<Integer, Map<Integer, List<Student>>> typeAgeMap = list.stream().collect(Collectors.groupingBy(Student::getType, Collectors.groupingBy(Student::getAge)));
  29. //分区
  30. //分成两部分,一部分大于10岁,一部分小于等于10岁
  31. Map<Boolean, List<Student>> partMap = list.stream().collect(Collectors.partitioningBy(v -> v.getAge() > 10));
  32. //规约
  33. Integer allAge = list.stream().map(Student::getAge).collect(Collectors.reducing(Integer::sum)).get(); //40

3.3.2 Collectors.toList() 解析

  1. //toList 源码
  2. public static <T> Collector<T, ?, List<T>> toList() {
  3. return new CollectorImpl<>((Supplier<List<T>>) ArrayList::new, List::add,
  4. (left, right) -> {
  5. left.addAll(right);
  6. return left;
  7. }, CH_ID);
  8. }
  9. //为了更好地理解,我们转化一下源码中的lambda表达式
  10. public <T> Collector<T, ?, List<T>> toList() {
  11. Supplier<List<T>> supplier = () -> new ArrayList();
  12. BiConsumer<List<T>, T> accumulator = (list, t) -> list.add(t);
  13. BinaryOperator<List<T>> combiner = (list1, list2) -> {
  14. list1.addAll(list2);
  15. return list1;
  16. };
  17. Function<List<T>, List<T>> finisher = (list) -> list;
  18. Set<Collector.Characteristics> characteristics = Collections.unmodifiableSet(EnumSet.of(Collector.Characteristics.IDENTITY_FINISH));
  19. return new Collector<T, List<T>, List<T>>() {
  20. @Override
  21. public Supplier supplier() {
  22. return supplier;
  23. }
  24. @Override
  25. public BiConsumer accumulator() {
  26. return accumulator;
  27. }
  28. @Override
  29. public BinaryOperator combiner() {
  30. return combiner;
  31. }
  32. @Override
  33. public Function finisher() {
  34. return finisher;
  35. }
  36. @Override
  37. public Set<Characteristics> characteristics() {
  38. return characteristics;
  39. }
  40. };
  41. }