演示stream用法,java8

embedded/2025/2/8 5:08:04/

展示如何使用Java 8的Stream API。这个示例包含了多个常见的Stream操作,如过滤、映射、排序、分组、去重等

  • filter是筛选出需要的元素
  • map是把流的对象元素映射为特定元素,可能是其中一个字段,或者是你自定义的类型
  • sortedStream()的方法,是新的元素列表的排序后的结果,不修改原有的列表!!
  • collect方法是收集前面操作完了之后的元素为一个列表,收集为列表完了之后可以调用列表方法,比如说是sort方法
  • groupingBy是分组,比如下面按照字符串长度归类不同字符串,长度为5的一组,长度为6的另外一组
java">List<String> words = Arrays.asList("apple", "banana", "cherry", "date", "elderberry");Map<Integer, List<String>> collect = words.stream().collect(Collectors.groupingBy(String::length));System.out.println(collect);

groupingBy着重讲解

统计每个年龄段的人叫什么名字

java">package AAA;import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;import static java.util.Collections.sort;
class Person {public String name;public int age;Person(String name, int age) {this.name = name;this.age = age;}public int getAge() {return age;}public String getName() {return name;}@Overridepublic String toString() {return name + ": " + age;}
}
public class Main5 {public static void main(String[] args) {// 创建一个Person对象列表List<Person> people = new ArrayList<>();// 手动插入100个Person对象people.add(new Person("Alice", 30));people.add(new Person("Bob", 25));people.add(new Person("Charlie", 30));people.add(new Person("David", 25));people.add(new Person("Eve", 28));people.add(new Person("Frank", 35));people.add(new Person("Grace", 40));people.add(new Person("Hannah", 22));people.add(new Person("Ian", 33));people.add(new Person("Jack", 29));people.add(new Person("Katherine", 62)); // 添加一个年龄大于60的人// 使用 groupingBy 按年龄范围分组// 使用 Stream 流和 groupingBy 方法进行分组Map<String, List<Person>> groupedByAgeRange = people.stream().collect(Collectors.groupingBy(person -> {int age = person.getAge();if (age > 18 && age < 25) {return "18-24";} else if (age >= 25 && age < 35) {return "25-34";} else if (age >= 35 && age < 60) {return "35-59";} else {return "60+";}}));// 打印分组结果groupedByAgeRange.forEach((ageRange, persons) -> {System.out.println("Age Range: " + ageRange);persons.forEach(System.out::println);System.out.println();});}
}

在这里插入图片描述
此外,还可以统计每个年龄段的有多少个人
Collectors.counting()是下游收集器,对分组之后的数据进行统计,更多的收集器类型可以参考https://docs.oracle.com/javase/8/docs/api/?java/lang/NullPointerException.html里面搜索Collectors类,里面的方法就是下游收集器

java">Map<String, Long> ageRangeCounts = people.stream().collect(Collectors.groupingBy(person -> {int age = person.getAge();if (age > 18 && age < 25) {return "18-24";} else if (age >= 25 && age < 35) {return "25-34";} else if (age >= 35 && age < 60) {return "35-59";} else {return "60+";}}, Collectors.counting())); // 使用 counting 方法统计每个分组的人数[^1^][^2^]// 打印每个年龄段的人数ageRangeCounts.forEach((ageRange, count) -> {System.out.println("Age Range: " + ageRange + ", Count: " + count);});
java">import java.util.*;
import java.util.stream.Collectors;// 定义一个Person类
class Person {String name;int age;Person(String name, int age) {this.name = name;this.age = age;}@Overridepublic String toString() {return name + ": " + age;}
}public class StreamExample {public static void main(String[] args) {// 创建一个Person对象列表List<Person> people = Arrays.asList(new Person("Alice", 30),new Person("Bob", 25),new Person("Charlie", 30),new Person("David", 25));// 1. 过滤出年龄大于25的人List<Person> filteredPeople = people.stream().filter(person -> person.age > 25).collect(Collectors.toList());System.out.println("Filtered People: " + filteredPeople);// 2. 按年龄分组Map<Integer, List<Person>> peopleByAge = people.stream().collect(Collectors.groupingBy(person -> person.age));System.out.println("People Grouped by Age: " + peopleByAge);// 3. 按姓名排序并打印people.stream().sorted(Comparator.comparing(person -> person.name)).forEach(System.out::println);// 4. 计算平均年龄double averageAge = people.stream().mapToInt(person -> person.age).average().orElse(0.0);System.out.println("Average Age: " + averageAge);// 5. 去重并打印姓名people.stream().map(person -> person.name).distinct().forEach(System.out::println);// 6. 使用并行流过滤年龄大于25的人List<Person> parallelFilteredPeople = people.parallelStream().filter(person -> person.age > 25).collect(Collectors.toList());System.out.println("Parallel Filtered People: " + parallelFilteredPeople);}
}

代码说明:
过滤操作:使用filter方法过滤出年龄大于25的人。
分组操作:使用collect方法和Collectors.groupingBy按年龄分组。
排序操作:使用sorted方法按姓名排序。
计算平均值:使用mapToInt和average方法计算平均年龄。
去重操作:使用map和distinct方法提取并去重姓名。
并行流:使用parallelStream进行并行过滤操作。
输出示例:
假设运行上述代码,输出可能如下:

Filtered People: [Alice: 30, Charlie: 30]
People Grouped by Age: {25=[Bob: 25, David: 25], 30=[Alice: 30, Charlie: 30]}
Alice: 30
Bob: 25
Charlie: 30
David: 25
Average Age: 27.5
Alice
Bob
Charlie
David
Parallel Filtered People: [Alice: 30, Charlie: 30]

自己又写了一个过滤,排序,收集为列表的Stream操作,下面分开sort了是因为collect(Collectors.toList())返回了可变列表,直接toList是不可变列表,toList而非collect(Collectors.toList())会导致无法sort,
单独list.forEach是因为println出现了二义性调用,只能单独list.forEach(System.out::println);

java">package AAA;import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;public class Main5 {public static void main(String[] args) {// 创建一个Person对象列表List<Person> people = new ArrayList<>();// 手动插入100个Person对象people.add(new Person("Alice", 30));people.add(new Person("Bob", 25));people.add(new Person("Charlie", 30));people.add(new Person("David", 25));people.add(new Person("Eve", 28));people.add(new Person("Frank", 35));people.add(new Person("Grace", 40));people.add(new Person("Hannah", 22));people.add(new Person("Ian", 33));people.add(new Person("Jack", 29));// 继续添加更多的Person对象...// 打印生成的Person对象列表List<Person> list = people.stream().filter(p -> p.getAge() > 20 && p.getAge() < 30).collect(Collectors.toList());list.sort(Comparator.comparing(Person::getName).thenComparing(Person::getAge));list.forEach(System.out::println);}
}

http://www.ppmy.cn/embedded/160467.html

相关文章

ASP.NET Core JWT

目录 Session的缺点 JWT&#xff08;Json Web Token&#xff09; 优点&#xff1a; 登录流程 JWT的基本使用 生成JWT 解码JWT 用JwtSecurityTokenHandler对JWT解码 注意 Session的缺点 对于分布式集群环境&#xff0c;Session数据保存在服务器内存中就不合适了&#…

通过docker安装部署deepseek以及python实现

前提条件 Docker 安装:确保你的系统已经安装并正确配置了 Docker。可以通过运行 docker --version 来验证 Docker 是否安装成功。 网络环境:保证设备有稳定的网络连接,以便拉取 Docker 镜像和模型文件。 步骤一:拉取 Ollama Docker 镜像 Ollama 可以帮助我们更方便地管理…

使用Cline+deepseek实现VsCode自动化编程

要在Visual Studio Code (VS Code)中实现自动化编程&#xff0c;特别是使用 Cline 和 Deepseek, 你需要先了解这两个工具的基本概念和功能。Cine 可能是一个打字错误或特定领域的工具 名称&#xff0c;而 Deepseek 可能指的是类似于深度学习模型或某种特定的代码搜索工具。这…

试试DeepSeek写prompt+stable diffusion生成漫画

#deepseek #stable diffusion 模型&#xff1a;dreamshaperXL_v21TurboDPMSDE.safetensors 一、情节拟定 漫画情节由deepseek自编自导&#xff0c;画幅为四张。 Prompt 1: 魔法觉醒 "一个平凡的少年在阁楼发现一本古老的魔法书&#xff0c;书页散发着微弱的蓝光。画…

详解python的单例模式

单例模式是一种设计模式&#xff0c;它确保一个类只有一个实例&#xff0c;并提供一个全局访问点来获取这个实例。在Python中实现单例模式有多种方法&#xff0c;下面我将详细介绍几种常见的实现方式。 1. 使用模块 Python的模块天然就是单例的&#xff0c;因为模块在第一次导…

qsort函数对二维数组的排序Cmp函数理解

在我们解题过程中&#xff0c;很多情况下&#xff0c;排序是必不可少的一环。 对于C语言来说&#xff0c;排序函数qsort就显得非常重要。 本文介绍一维数组、二维数组的qsort排序&#xff0c;其中二维数组的Cmp函数的写法做了详细注释。 qsort函数原型介绍&#xff1a; /* …

rabbitMQ消息转换器

消息转换器 Spring的消息发送代码接收的消息体是一个Object&#xff1a; 而在数据传输时&#xff0c;它会把你发送的消息序列化为字节发送给MQ&#xff0c;接收消息的时候&#xff0c;还会把字节反序列化为Java对象。 只不过&#xff0c;默认情况下Spring采用的序列化方式是J…

配置@别名路径,把@/ 解析为 src/

路径解析配置 webpack 安装 craco npm i -D craco/craco 项目根目录下创建文件 craco.config.js &#xff0c;内容如下 const path require(path) module.exports {webpack: {// 配置别名alias: {// 约定&#xff1a; 使用 表示src文件所在路径: path.resolve(__dirname,src)…