【Lambda】java之lambda表达式&stream流式编程操作集合

java之lambda表达式&stream流式编程操作集合

  • [1 stream流概念](#1 stream流概念)
    • [1.1 中间操作](#1.1 中间操作)
      • [1.1.1 无状态操作](#1.1.1 无状态操作)
      • [1.1.2 有状态操作](#1.1.2 有状态操作)
    • [1.2 终端操作](#1.2 终端操作)
      • [1.2.1 非短路操作](#1.2.1 非短路操作)
      • [1.2.2 短路操作](#1.2.2 短路操作)
  • [2 steam流的生成](#2 steam流的生成)
    • [2.1 方式一:数组转为stream流](#2.1 方式一:数组转为stream流)
    • [2.2 方式二:集合转为steam流](#2.2 方式二:集合转为steam流)
    • [2.3 方式三:Stream.builder创建stream流](#2.3 方式三:Stream.builder创建stream流)
    • [2.4 方式四:使用 Stream.of 方法](#2.4 方式四:使用 Stream.of 方法)
    • [2.5 方式五:从文件创建流](#2.5 方式五:从文件创建流)
    • [2.6 方式六:生成无限流](#2.6 方式六:生成无限流)
  • [3 无状态的中间操作](#3 无状态的中间操作)
    • [3.1 distinct](#3.1 distinct)
    • [3.2 limit](#3.2 limit)
    • [3.3 skip](#3.3 skip)
    • [3.4 sorted](#3.4 sorted)
    • [3.5 组合使用](#3.5 组合使用)
  • [4 有状态的中间操作](#4 有状态的中间操作)
    • [4.1 filter(重要)](#4.1 filter(重要))
    • [4.2 map(重要)](#4.2 map(重要))
    • [4.3 flatMap](#4.3 flatMap)
    • [4.4 peek](#4.4 peek)
    • [4.5 组合使用](#4.5 组合使用)
  • [5 非短路的终端操作](#5 非短路的终端操作)
    • [5.1 forEach](#5.1 forEach)
    • [5.2 toArray](#5.2 toArray)
    • [5.3 reduce(重要)](#5.3 reduce(重要))
    • [5.4 collect(重要)](#5.4 collect(重要))
    • [5.5 min、max、count](#5.5 min、max、count)
    • [5.6 组合使用](#5.6 组合使用)
  • [6 短路的终端操作](#6 短路的终端操作)
    • [6.1 anyMatch](#6.1 anyMatch)
    • [6.2 allMatch](#6.2 allMatch)
    • [6.3 noneMatch](#6.3 noneMatch)
    • [6.4 findFirst](#6.4 findFirst)
    • [6.5 findAny](#6.5 findAny)
    • [6.6 组合使用](#6.6 组合使用)
  • [7 并行流](#7 并行流)
  • [8 总结](#8 总结)

1 stream流概念

简单来讲,Stream流是一种用于处理数据集合的高级迭代器,它可以对集合中的元素进行各种操作,如过滤、映射、排序等。通过使用Stream API,我们可以以声明式的方式处理数据,而无需显式地编写循环和条件语句。

Stream流的操作分为两种,中间操作终端操作

1.1 中间操作

是指对每个元素独立进行操作,不依赖于其他元素的状态。它们不会改变流中的元素本身,而是创建一个新的Stream对象来表示转换后的结果。常见的无状态中间操作有map、filter、flatMap等。

1.1.1 无状态操作

无状态操作不会改变流中的元素,也不会改变流的状态;这些操作可以并行执行,因为它们不依赖于流中的其他元素。例如:

  • distinct:返回去重的Stream。
  • limit:限制从流中获得前n个数据,返回前n个元素数据组成的Stream流。
  • skip:跳过前n个数据,返回第n个元素后面数据组成的Stream。sorted:返回一个排序的Stream。

1.1.2 有状态操作

有状态操作会改变流的状态,或者依赖于流中的其他元素。这些操作不能并行执行,因为它们需要访问或修改流的状态。

例如:

  • filter:过滤流,过滤流中的元素,返回一个符合条件的Stream
  • map:转换流,将一种类型的流转换为另外一种流。(mapToInt、mapToLong、mapToDouble 返回int、long、double基本类型对应的Stream)
  • peek:主要用来查看流中元素的数据状态,该方法主要用于调试,方便debug查看Stream内进行处理的每个元素。仅在对流内元素进行操作时,peek才会被调用,当不对元素做任何操作时,peek自然也不会被调用了
  • flatMap:简单的说,就是一个或多个流合并成一个新流。

1.2 终端操作

是对数据进行最终处理的操作,它们会消耗掉Stream并产生一个结果或者副作用(如输出到控制台)。一旦执行了终端操作,Stream就不能再被使用。常见的终端操作有collect、forEach、reduce等。

1.2.1 非短路操作

非短路操作会处理流中的所有元素,并返回一个结果。

如:

  • forEach:循环操作Stream中数据。
  • toArray:返回流中元素对应的数组对象。
  • reduce:聚合操作,用来做统计,将流中元素反复结合起来统计计算,得到一个值.。
  • collect:聚合操作,封装目标数据,将流转换为其他形式接收,如:List、Set、Map、Array。
  • min、max、count:聚合操作,最小值,最大值,总数量。

1.2.2 短路操作

短路操作会在满足某个条件时提前结束处理,并返回一个结果。例如:

  • anyMatch:短路操作,有一个符合条件返回true。
  • allMatch:所有数据都符合条件返回true。
  • noneMatch:所有数据都不符合条件返回true。
  • findFirst:短路操作,获取第一个元素。
  • findAny:短路操作,获取任一元素。

2 steam流的生成

2.1 方式一:数组转为stream流

java 复制代码
int [] arr = {1,2,3,4,5,6,7,8,9,10};       
Arrays.stream(arr).forEach(System.out::println);

2.2 方式二:集合转为steam流

java 复制代码
List<Integer> list = Arrays.asList(1,2,3,4,5,6,7,8,9,10);
list.stream().forEach(System.out::println);

2.3 方式三:Stream.builder创建stream流

Stream.builder创建stream流,允许你逐步构建一个流

java 复制代码
Stream.Builder<Integer> builder = Stream.builder();

// 添加元素到流中
builder.add(1);
builder.add(2);
builder.add(3);

// 构建流
Stream<Integer> stream = builder.build();

// 使用流
stream.forEach(System.out::println);

2.4 方式四:使用 Stream.of 方法

Stream.of 方法可以接受一系列元素并返回一个包含这些元素的流。

java 复制代码
Stream<String> words = Stream.of("apple", "banana", "orange");
words.forEach(System.out::println);

2.5 方式五:从文件创建流

可以使用 Files.lines 方法从文件中创建流。

java 复制代码
try (Stream<String> stream = Files.lines(Paths.get("file.txt"))) {
    stream.forEach(System.out::println);
} catch (IOException e) {
    e.printStackTrace();
}

2.6 方式六:生成无限流

可以使用 Stream.generate 或 Stream.iterate 方法来生成无限流。

java 复制代码
Stream<Double> randomNumbers = Stream.generate(Math::random);
randomNumbers.forEach(System.out::println);
Stream<Integer> oddNumbers = Stream.iterate(1, n -> n + 2);
oddNumbers.forEach(System.out::println);

3 无状态的中间操作

3.1 distinct

返回一个去重的流,即去除重复的元素

java 复制代码
Stream<Integer> distinctStream = Stream.of(1, 2, 2, 3, 4, 4, 5).distinct();
distinctStream.forEach(System.out::println);
//输出结果:12345

3.2 limit

限制从流中获得前n个数据,返回前n个元素数据组成的流

java 复制代码
Stream<Integer> limitedStream = Stream.of(1, 2, 3, 4, 5).limit(3);
limitedStream.forEach(System.out::println);
//输出结果:123

3.3 skip

java 复制代码
Stream<Integer> skippedStream = Stream.of(1, 2, 3, 4, 5).skip(2);
skippedStream.forEach(System.out::println);
//输出结果:345

3.4 sorted

java 复制代码
//正序排序从小到大
Stream<Integer> sortedStream = Stream.of(5, 3, 1, 4, 2).sorted();
sortedStream.forEach(System.out::println);
//输出结果:12345

//逆序排序从大到小,Comparator.reverseOrder() 创建了一个逆序比较器,然后传递给 sorted 方法,从而实现了逆序排序
Stream<Integer> sortedStream = Stream.of(5, 3, 1, 4, 2).sorted(Comparator.reverseOrder());
sortedStream.forEach(System.out::println);
//输出结果:54321

3.5 组合使用

这段代码首先去重,然后排序,跳过第一个元素,最后限制结果流只包含前三个元素

java 复制代码
Stream<Integer> stream = Stream.of(1, 2, 2, 3, 4, 4, 5)
                .distinct()
                .sorted()
                .skip(1)
                .limit(3);
stream.forEach(System.out::println);
//输出结果:234

测试实体类:

java 复制代码
@Data
public class Person {
    private int id;
    private String name; // 姓名
    private int salary; // 薪资
    private int age; // 年龄
    private String sex; //性别
    private String area; // 地区
    private List<Person> employeeList; //下属

    public Person() {
    }

    // 构造方法
    public Person(String name, int salary, int age,String sex,String area) {
        this.name = name;
        this.salary = salary;
        this.age = age;
        this.sex = sex;
        this.area = area;
    }

    // 构造方法
    public Person(int id, String name, int salary, int age, String sex, String area) {
        this.id = id;
        this.name = name;
        this.salary = salary;
        this.age = age;
        this.sex = sex;
        this.area = area;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", salary=" + salary +
                ", age=" + age +
                ", sex='" + sex + '\'' +
                ", area='" + area + '\'' +
                '}';
    }
}
java 复制代码
// 创建一个List来存储Person对象
List<Person> personList = new ArrayList<>();

// 创建8个Person对象并添加到列表中
personList.add(new Person(1, "Alice", 5000, 30, "Female", "New York"));
personList.add(new Person(2, "Bob", 6000, 35, "Male", "Los Angeles"));
personList.add(new Person(3, "Charlie", 5500, 28, "Male", "Chicago"));
personList.add(new Person(4, "Diana", 7000, 40, "Female", "Miami"));
personList.add(new Person(5, "Ethan", 4800, 25, "Male", "Houston"));
personList.add(new Person(6, "Fiona", 5300, 32, "Female", "Seattle"));
personList.add(new Person(7, "George", 6200, 38, "Male", "Boston"));
personList.add(new Person(8, "Hannah", 5900, 29, "Female", "San Francisco"));

4 有状态的中间操作

4.1 filter(重要)

java 复制代码
// 1. 筛选出所有女性
List<Person> females = personList.stream()
        .filter(person -> person.getSex().equalsIgnoreCase("female"))
        .collect(Collectors.toList());
System.out.println("女性列表: " + females);

// 2. 筛选出薪资高于5000的人员
List<Person> highSalary = personList.stream()
        .filter(person -> person.getSalary() > 5000)
        .collect(Collectors.toList());
System.out.println("薪资高于5000的人员: " + highSalary);

// 3. 筛选出年龄在30岁及以上的人员
List<Person> ageAbove30 = personList.stream()
        .filter(person -> person.getAge() >= 30)
        .collect(Collectors.toList());
System.out.println("年龄在30岁及以上的人员: " + ageAbove30);

// 4. 筛选出居住在特定城市(例如"New York")的人员
List<Person> livingInNewYork = personList.stream()
        .filter(person -> person.getArea().equalsIgnoreCase("New York"))
        .collect(Collectors.toList());
System.out.println("居住在纽约的人员: " + livingInNewYork);

// 5. 筛选出名字以"A"开头的人员
List<Person> namesStartingWithA = personList.stream()
        .filter(person -> person.getName().startsWith("A"))
        .collect(Collectors.toList());
System.out.println("名字以A开头的人员: " + namesStartingWithA);

4.2 map(重要)

java 复制代码
// 1. 提取所有人的名字
List<String> names = personList.stream()
        .map(Person::getName)
        .collect(Collectors.toList());
System.out.println("所有人的名字: " + names);

// 2. 提取所有人的薪资
List<Integer> salaries = personList.stream()
        .map(Person::getSalary)
        .collect(Collectors.toList());
System.out.println("所有人的薪资: " + salaries);

// 3. 提取所有人的地区
List<String> cities = personList.stream()
        .map(Person::getArea)
        .collect(Collectors.toList());
System.out.println("所有人的城市: " + cities);

// 4. 提取所有人的年龄,并加上10
List<Integer> agesPlus10 = personList.stream()
        .map(person -> person.getAge() + 10)
        .collect(Collectors.toList());
System.out.println("所有人的年龄加十: " + agesPlus10);

// 5. 提取薪资信息并格式化为字符串
List<String> salaryInfo = personList.stream()
        .map(person -> person.getName() + "的薪资为: " + person.getSalary())
        .collect(Collectors.toList());
System.out.println("薪资信息: " + salaryInfo);

4.3 flatMap

flatMap 是 Java Stream API 中的一个非常有用的方法,通常用于将多个流扁平化为一个流。在处理 Person 对象时,我们可以使用 flatMap 来进行一些复杂的数据结构操作。以下是一些示例,展示如何在你的 personList 上使用 flatMap。

java 复制代码
List<String> flatMappedList = personList.stream()
                                .map(person -> person.getName()) // 将Person对象转换为名字
                                .flatMap(name -> Stream.of(name, name.toUpperCase())) // 将名字转换为名字和名字的大写形式
                                .collect(Collectors.toList());

4.4 peek

java 复制代码
personList.stream()
          .peek(person -> System.out.println("Before filter: " + person)) // 打印每个Person对象
          .filter(person -> person.getAge() > 30) // 过滤年龄大于30的Person对象
          .peek(person -> System.out.println("After filter: " + person)) // 打印过滤后的Person对象
          .collect(Collectors.toList());

4.5 组合使用

java 复制代码
// 使用Stream API处理personList
List<String> combinedList = personList.stream()
                                      .filter(person -> person.getAge() > 30) // 过滤年龄大于30的Person对象
                                      .map(person -> person.getName()) // 将Person对象转换为名字
                                      .flatMap(name -> Stream.of(name, name.toUpperCase())) // 将名字转换为名字和名字的大写形式
                                      .peek(System.out::println) // 打印每个名字
                                      .collect(Collectors.toList()); // 收集结果到一个List中

5 非短路的终端操作

5.1 forEach

java 复制代码
personList.stream()
          .forEach(person -> System.out.println(person.getName()));

personList.stream()
          .forEachOrdered(person -> System.out.println(person.getName()));

5.2 toArray

java 复制代码
Object[] array = personList.stream().toArray();

5.3 reduce(重要)

java 复制代码
// 1. 计算总薪资
int totalSalary = personList.stream()
        .map(Person::getSalary)
        .reduce(0, Integer::sum);
System.out.println("总薪资: " + totalSalary);

// 2. 计算平均薪资
Optional<Double> averageSalary = personList.stream()
        .map(Person::getSalary)
        .reduce((a, b) -> a + b)
        .map(sum -> sum / (double) personList.size());
System.out.println("平均薪资: " + averageSalary.orElse(0.0));

// 3. 查找最高薪资
Optional<Integer> maxSalary = personList.stream()
        .map(Person::getSalary)
        .reduce(Integer::max);
System.out.println("最高薪资: " + maxSalary.orElse(0));

// 4. 查找最低薪资
Optional<Integer> minSalary = personList.stream()
        .map(Person::getSalary)
        .reduce(Integer::min);
System.out.println("最低薪资: " + minSalary.orElse(0));

// 5. 计算年龄之和
int totalAge = personList.stream()
        .map(Person::getAge)
        .reduce(0, Integer::sum);
System.out.println("总年龄: " + totalAge);

5.4 collect(重要)

java 复制代码
//收集到 List集合
List<String> names = personList.stream()
        .map(Person::getName)
        .collect(Collectors.toList());
//收集到 Set集合
Set<String> cities = personList.stream()
        .map(Person::getArea)
        .collect(Collectors.toSet());
//收集到 Map集合
Map<Integer, String> idToNameMap = personList.stream()
        .collect(Collectors.toMap(Person::getId, Person::getName));

Map<Integer, Person> idToPersonMap = personList.stream()
        .collect(Collectors.toMap(Person::getId, v -> v));
//收集到自定义集合
List<Person> sortedList = personList.stream()
        .sorted(Comparator.comparing(Person::getAge))
        .collect(Collectors.toList());
//使用 Collectors.joining 连接字符串
String namesString = personList.stream()
        .map(Person::getName)
        .collect(Collectors.joining(", "));
//Collectors.groupingBy
Map<String, List<Person>> cityToPeopleMap = personList.stream()
        .collect(Collectors.groupingBy(Person::getArea));
//使用 Collectors.partitioningBy 分区
Map<Boolean, List<Person>> isAdultMap = personList.stream()
        .collect(Collectors.partitioningBy(person -> person.getAge() >= 18));
//使用 Collectors.summarizingInt 计算统计信息
IntSummaryStatistics salarySummary = personList.stream()
        .collect(Collectors.summarizingInt(Person::getSalary));
double average = salarySummary.getAverage();//平均值
int max = salarySummary.getMax();//最大值
long count = salarySummary.getCount();//计数
int min = salarySummary.getMin();//最小值
long sum = salarySummary.getSum();//求和

5.5 min、max、count

java 复制代码
Optional<Person> maxSalaryPerson = personList.stream()
                                             .max(Comparator.comparing(Person::getSalary));
Optional<Person> minAgePerson = personList.stream()
                                          .min(Comparator.comparing(Person::getAge));
long count = personList.stream().count();

5.6 组合使用

这段代码首先计算所有 Person 对象的工资总和,然后找到年龄最大的 Person 对象,最后将所有 Person 对象的名字收集到一个 List 中。

java 复制代码
int totalSalary = personList.stream()
                            .mapToInt(Person::getSalary)
                            .reduce(0, Integer::sum);

Optional<Person> oldestPerson = personList.stream()
                                         .max(Comparator.comparing(Person::getAge));

List<String> names = personList.stream()
                               .map(Person::getName)
                               .collect(Collectors.toList());

6 短路的终端操作

6.1 anyMatch

java 复制代码
boolean hasFemale = personList.stream()
                              .anyMatch(person -> "Female".equals(person.getGender()));

6.2 allMatch

java 复制代码
boolean allAdults = personList.stream()
                              .allMatch(person -> person.getAge() >= 18);

6.3 noneMatch

java 复制代码
boolean noRetired = personList.stream()
                              .noneMatch(person -> person.getAge() >= 65);

6.4 findFirst

java 复制代码
Optional<Person> firstPerson = personList.stream()
                                         .findFirst();

6.5 findAny

java 复制代码
Optional<Person> anyPerson = personList.stream()
                                       .findAny();

6.6 组合使用

这段代码首先检查是否存在女性,然后找到第一个成年人,最后将所有成年人的名字收集到一个 List 中。

java 复制代码
boolean hasFemale = personList.stream()
                              .anyMatch(person -> "Female".equals(person.getGender()));

Optional<Person> firstAdult = personList.stream()
                                        .filter(person -> person.getAge() >= 18)
                                        .findFirst();

List<String> names = personList.stream()
                               .filter(person -> person.getAge() >= 18)
                               .map(Person::getName)
                               .collect(Collectors.toList());

7 并行流

并行流可以提高处理大数据集时的性能。Java Stream API 的并行处理是基于 Java 的 Fork/Join 框架实现的。Fork/Join 框架是 Java 7 引入的一种并行计算框架,它可以将一个大任务拆分成多个小任务,然后在多个处理器上并行执行这些小任务,最后将结果合并。

在 Stream API 中,并行流是通过 parallelStream() 方法创建的。当你调用 parallelStream() 方法时,Stream API 会创建一个 ForkJoinTask,并将其提交给 ForkJoinPool 执行。ForkJoinPool 是一个特殊的线程池,它使用工作窃取算法来平衡任务执行,从而提高并行处理效率。

java 复制代码
String allNames = personList.parallelStream()
    .map(Person::getName)
    .collect(Collectors.joining(", "));
System.out.println("All Names (Parallel): " + allNames);

8 总结

Stream API 的主要特点包括:

  1. 简洁性:Stream API 提供了一种简洁的方式来处理集合数据,使得代码更加易读、易写。
  2. 可读性:Stream API 的操作可以链式调用,使得代码更加清晰、易读。并行处理:Stream API 支持
  3. 并行处理,可以充分利用多核处理器的能力。
  4. 惰性求值:Stream API 的操作是惰性求值的,即只有在需要结果时才会执行操作。
  5. 无状态操作:Stream API 的无状态操作不会改变流中的元素,也不会改变流的状态。
  6. 有状态操作:Stream API 的有状态操作会改变流的状态,或者依赖于流中的其他元素。
  7. 短路操作:Stream API 的短路操作会在满足某个条件时提前结束处理,并返回一个结果。
  8. 终端操作:Stream API 的终端操作会处理流中的所有元素,并返回一个结果。

创作不易,不妨点赞、收藏、关注支持一下,各位的支持就是我创作的最大动力❤️