当前位置: 首页 > news >正文

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

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

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

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

2.2 方式二:集合转为steam流

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流,允许你逐步构建一个流

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 方法可以接受一系列元素并返回一个包含这些元素的流。

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

2.5 方式五:从文件创建流

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

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 方法来生成无限流。

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

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

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

3.2 limit

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

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

3.3 skip

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

3.4 sorted

//正序排序从小到大
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 组合使用

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

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

测试实体类:

@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;}@Overridepublic String toString() {return "Person{" +"name='" + name + '\'' +", salary=" + salary +", age=" + age +", sex='" + sex + '\'' +", area='" + area + '\'' +'}';}
}
// 创建一个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(重要)

// 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(重要)

// 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。

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

4.4 peek

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 组合使用

// 使用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

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

5.2 toArray

Object[] array = personList.stream().toArray();

5.3 reduce(重要)

// 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(重要)

//收集到 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

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 中。

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

boolean hasFemale = personList.stream().anyMatch(person -> "Female".equals(person.getGender()));

6.2 allMatch

boolean allAdults = personList.stream().allMatch(person -> person.getAge() >= 18);

6.3 noneMatch

boolean noRetired = personList.stream().noneMatch(person -> person.getAge() >= 65);

6.4 findFirst

Optional<Person> firstPerson = personList.stream().findFirst();

6.5 findAny

Optional<Person> anyPerson = personList.stream().findAny();

6.6 组合使用

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

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 是一个特殊的线程池,它使用工作窃取算法来平衡任务执行,从而提高并行处理效率。

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 的终端操作会处理流中的所有元素,并返回一个结果。

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

在这里插入图片描述

http://www.lryc.cn/news/502393.html

相关文章:

  • 家具购物小程序+php
  • 【GIS教程】使用GDAL-Python将tif转为COG并在ArcGIS Js前端加载-附完整代码
  • VB.net进行CAD二次开发(二)与cad交互
  • 【NLP 11、Adam优化器】
  • 51单片机应用开发(进阶)---串口接收字符命令
  • redis 怎么样删除list
  • 【数据结构——内排序】快速排序(头歌实践教学平台习题)【合集】
  • npm或yarn包配置地址源
  • STUN服务器用于内网NAT的方案
  • Linux 简单命令总结
  • Vue.js组件开发:提升你的前端工程能力
  • 使用 Pandas 读取 JSON 数据的五种常见结构解析
  • C++鼠标轨迹算法(鼠标轨迹模拟真人移动)
  • Go mysql驱动源码分析
  • GNSS误差源及差分定位
  • pg数据类型
  • 【java】finalize方法
  • HNU_多传感器(专选)_作业4(构建单层感知器实现分类)
  • 以太网链路详情
  • vue3 setup语法,子组件点击一个元素打印了这个元素的下标id,怎么传递给父组件,让父组件去使用
  • 《Keras3 minist 手写数字AI模型训练22秒精度达到:0.97》
  • 【.net core】【sqlsugar】大数据写入配置(需要版本5.0.45)
  • ansible运维实战
  • DDOS分布式拒绝服务攻击
  • 如何使用 Python 实现 UDP 通信?
  • MTK 配置文件梳理
  • 论文笔记:Treat Visual Tokens as Text? But Your MLLM Only Needs Fewer Efforts to See
  • 软考高级架构 —— 10.6 大型网站系统架构演化实例 + 软件架构维护
  • 2024美赛数学建模C题:网球比赛中的动量,用马尔可夫链求解!详细分析
  • 23种设计模式之状态模式