Stream流的常用方法(自用)
自用的笔记, 有🚩 需要多看
基本数据
自定义实体
@Data
class Student{private String name;private Integer age;private Double height;public Student() {}
}
假数据
Student s1 = new Student();
s1.setAge(20);
s1.setName("cookie");
s1.setHeight(180d);Student s2 = new Student();
s2.setAge(30);
s2.setName("cookie");
s2.setHeight(180d);Student s3 = new Student();
s3.setAge(40);
s3.setName("bob");
s3.setHeight(175d);Student s4 = new Student();
s4.setAge(40);
s4.setName("bob");
s4.setHeight(180d);// 存入list集合
List<Student> list = new ArrayList<>();
list.add(s1);
list.add(s2);
list.add(s3);
list.add(s4);
一, 分组
1. 一层分组/简单分组
/*** 需求一(一层分组):根据Age分组*/
System.out.println("需求一(一层分组):根据Age分组");
Map<Integer, List<Student>> collect = list.stream().collect(Collectors.groupingBy(Student::getAge));
for (Integer age : collect.keySet()) {System.out.println("key:" + age + "\tvalue:" + collect.get(age));
}/*** 控制台结果:* key:20 value:[Student(name=cookie, age=20, height=180.0)]* key:40 value:[Student(name=bob, age=40, height=175.0), Student(name=bob, age=40, height=180.0)]* key:30 value:[Student(name=cookie, age=30, height=180.0)]*/
2. 多层分组
/*** 需求二: 先根据name分组,然后再根据身高分组*/
System.out.println("需求二: 先根据name分组,然后再根据身高分组");
Map<String, Map<Double, List<Student>>> collect1 = list.stream().collect(Collectors.groupingBy(Student::getName, Collectors.groupingBy(Student::getHeight)));
Set<String> namesGroup = collect1.keySet();
for (String namekey : namesGroup) {Map<Double, List<Student>> heightGroupMap = collect1.get(namekey);Set<Double> height = heightGroupMap.keySet();for (Double h : height) {System.out.println("name:" + namekey + " height:" + heightGroupMap.get(h));}
}/*** 控制台结果:* name:bob height:[Student(name=bob, age=40, height=175.0)]* name:bob height:[Student(name=bob, age=40, height=180.0)]* name:cookie height:[Student(name=cookie, age=20, height=180.0), Student(name=cookie, age=30, height=180.0)]*/
3. 多层分组-自定义key 🚩
/*** 需求三: 自定义key返回 形式如下: age_height bob_175*/
System.out.println("需求三: 自定义key返回 形式如下: age_height bob_175");
Map<String, List<Student>> collect2 = list.stream().collect(Collectors.groupingBy(c -> c.getName() + "_" + c.getHeight()));for (String customKey : collect2.keySet()) {System.out.println("key:" + customKey +" value:"+ collect2.get(customKey));
}
/*** 控制台结果:* key:bob_180.0 value:[Student(name=bob, age=40, height=180.0)]* key:bob_175.0 value:[Student(name=bob, age=40, height=175.0)]* key:cookie_180.0 value:[Student(name=cookie, age=20, height=180.0), Student(name=cookie, age=30, height=180.0)]*/
二, 排序
方式一: 通过自定义的比较器(非必要不推荐)
/**
* 需求: 根据身高排序,如果身高相同,根据年龄排序,如果年龄依然相同,根据名称字母顺序排序
*/
List<Student> collect3 = list.stream().sorted(new Comparator<Student>() {@Overridepublic int compare(Student o1, Student o2) {// 这里前面的减去后面的是升序, 反之这是降序if (!o1.getHeight().equals(o2.getHeight())) {return (int) (o1.getHeight() - o2.getHeight());}if (!o1.getAge().equals(o2.getAge())) {return o1.getAge() - o2.getAge();}return o1.getName().compareTo(o2.getName());}
}).collect(Collectors.toList());
System.out.println(collect3);/*** 控制台结果:* [Student(name=bob, age=40, height=175.0), * Student(name=cookie, age=20, height=180.0), * Student(name=cookie, age=30, height=180.0), * Student(name=bob, age=40, height=180.0)]*/// 注: 当然上面的也可以做一个简化
List<Student> collect3 = list.stream().sorted((o1, o2) -> {// 这里前面的减去后面的是升序, 反之这是降序if (!o1.getHeight().equals(o2.getHeight())) {return (int) (o1.getHeight() - o2.getHeight());}if (!o1.getAge().equals(o2.getAge())) {return o1.getAge() - o2.getAge();}return o1.getName().compareTo(o2.getName());
}).collect(Collectors.toList());
方式二: 通过lambda 🚩
List<Student> collect4 = list.stream().sorted(Comparator.comparingDouble(Student::getHeight).thenComparingInt(Student::getAge).thenComparing(Student::getName)).collect(Collectors.toList());
System.out.println(collect4);/*** 控制台结果:* [Student(name=bob, age=40, height=175.0), * Student(name=cookie, age=20, height=180.0), * Student(name=cookie, age=30, height=180.0), * Student(name=bob, age=40, height=180.0)]*/// 注意:
// 方式一,升序降序是通过返回的正负,
// 方式二而是通过方法, 现在我们首先通过身高降序, 我们只需要在条件的后面加一个reversed()后缀方法即可List<Student> collect4 = list.stream().sorted(Comparator.comparingDouble(Student::getHeight).reversed().thenComparingInt(Student::getAge).thenComparing(Student::getName)
).collect(Collectors.toList());
System.out.println(collect4);/*** 修改之后控制台结果:* [Student(name=cookie, age=20, height=180.0), * Student(name=cookie, age=30, height=180.0), * Student(name=bob, age=40, height=180.0), * Student(name=bob, age=40, height=175.0)]*/
三, 统计
/*** 需求: 统计年龄之和*/
int ageSum = list.stream().mapToInt(Student::getAge).sum();/*** 求年龄平均值*/
Double ageAvg1 = list.stream().collect(Collectors.averagingInt(Student::getAge));
// 或者
double ageAvg2 = list.stream().mapToInt(Student::getAge).average().getAsDouble();/*** 求年龄最大值*/
int maxAge = list.stream().mapToInt(Student::getAge).max().getAsInt();/*** 最小值*/
int minAge = list.stream().mapToInt(Student::getAge).min().getAsInt();
缓慢总结中~~~~
相关文章:
Stream流的常用方法(自用)
自用的笔记, 有🚩 需要多看 基本数据 自定义实体 Data class Student{private String name;private Integer age;private Double height;public Student() {} }假数据 Student s1 new Student(); s1.setAge(20); s1.setName("cookie"); s1.setHeight(…...
【python函数】torch.nn.Embedding函数用法图解
学习SAM模型的时候,第一次看见了nn.Embedding函数,以前接触CV比较多,很少学习词嵌入方面的,找了一些资料一开始也不是很理解,多看了两遍后,突然顿悟,特此记录。 SAM中PromptEncoder中运用nn.Emb…...
with ldid... /opt/MonkeyDev/bin/md: line 326: ldid: command not found
吐槽傻逼xcode 根据提示 执行了这个脚本/opt/MonkeyDev/bin/md 往这里面添加你brew install 安装文件的目录即可...
[golang gui]fyne框架代码示例
1、下载GO Go语言中文网 golang安装包 - 阿里镜像站(镜像站使用方法:查找最新非rc版本的golang安装包) golang安装包 - 中科大镜像站 go二进制文件下载 - 南京大学开源镜像站 Go语言官网(Google中国) Go语言官网(Go团队) 截至目前(2023年9月17日&#x…...
2000-2018年各省能源消费和碳排放数据
2000-2018年各省能源消费和碳排放数据 1、时间:2000-2018年 2、范围:30个省市 3、指标:id、year、ENERGY、COAL、碳排放倒数*100 4、来源:能源年鉴 5、指标解释: 2018年碳排放和能源数据为插值法推算得到 碳排放…...
C# ref 学习1
ref 关键字用在四种不同的上下文中; 1.在方法签名和方法调用中,按引用将参数传递给方法。 2.在方法签名中,按引用将值返回给调用方。 3.在成员正文中,指示引用返回值是否作为调用方欲修改的引用被存储在本地,或在一般…...
MQ - 08 基础篇_消费者客户端SDK设计(下)
文章目录 导图Pre概述消费分组协调者消费分区分配策略轮询粘性自定义消费确认确认后删除数据确认后保存消费进度数据消费失败处理从服务端拉取数据失败本地业务数据处理失败提交位点信息失败总结导图 Pre...
Flutter层对于Android 13存储权限的适配问题
感觉很久没有写博客了,不对,的确是很久没有写博客了。原因我不怎么想说,玩物丧志了。后面渐渐要恢复之前的写作节奏。今天来聊聊我最近遇到的一个问题: Android 13版本对于storage权限的控制问题。 我们都知道,Andro…...
Android kotlin开源项目-功能标题目录
目录 一、BRVAH二、开源项目1、RV列表动效(标题目录)2、拖拽与侧滑(标题目录)3、数据库(标题目录)4、树形图(多级菜单)(标题目录)5、轮播图与头条(标题目录)6…...
Linux下,基于TCP与UDP协议,不同进程下单线程通信服务器
C语言实现Linux下,基于TCP与UDP协议,不同进程下单线程通信服务器 一、TCP单线程通信服务器 先运行server端,再运行client端输入"exit" 是退出 1.1 server_TCP.c **#include <my_head.h>#define PORT 6666 #define IP &qu…...
qt功能自己创作
按钮按下三秒禁用 void MainWindow::on_pushButton_5_clicked(){// 锁定界面setWidgetsEnabled(ui->centralwidget, false);// 创建一个定时器,等待3秒后解锁界面QTimer::singleShot(3000, this, []() {setWidgetsEnabled(ui->centralwidget, true);;//ui-&g…...
Linux网络编程:使用UDP和TCP协议实现网络通信
目录 一. 端口号的概念 二. 对于UDP和TCP协议的认识 三. 网络字节序 3.1 字节序的概念 3.2 网络通信中的字节序 3.3 本地地址格式和网络地址格式 四. socket编程的常用函数 4.1 sockaddr结构体 4.2 socket编程常见函数的功能和使用方法 五. UDP协议实现网络通信 5.…...
【后端速成 Vue】初识指令(上)
前言: Vue 会根据不同的指令,针对标签实现不同的功能。 在 Vue 中,指定就是带有 v- 前缀 的特殊 标签属性,比如: <div v-htmlstr> </div> 这里问题就来了,既然 Vue 会更具不同的指令&#…...
爬虫 — Scrapy-Redis
目录 一、背景1、数据库的发展历史2、NoSQL 和 SQL 数据库的比较 二、Redis1、特性2、作用3、应用场景4、用法5、安装及启动6、Redis 数据库简单使用7、Redis 常用五大数据类型7.1 Redis-String7.2 Redis-List (单值多value)7.3 Redis-Hash7.4 Redis-Set (不重复的)7.5 Redis-Z…...
tcpdump常用命令
需要安装 tcpdump wireshark ifconfig找到网卡名称 eth0, ens192... tcpdump需要root权限 网卡eth0 经过221.231.92.240:80的流量写入到http.cap tcpdump -i eth0 host 221.231.92.240 and port 80 -vvv -w http.cap ssh登录到主机查看排除ssh 22端口的报文 tcpdump -i …...
计算机网络运输层网络层补充
1 CDMA是码分多路复用技术 和CMSA不是一个东西 UPD是只确保发送 但是接收端收到之后(使用检验和校验 除了检验的部分相加 对比检验和是否相等。如果不相同就丢弃。 复用和分用是发生在上层和下层的问题。通过比如时分多路复用 频分多路复用等。TCP IP 应用层的IO多路复用。网…...
java CAS详解(深入源码剖析)
CAS是什么 CAS是compare and swap的缩写,即我们所说的比较交换。该操作的作用就是保证数据一致性、操作原子性。 cas是一种基于锁的操作,而且是乐观锁。在java中锁分为乐观锁和悲观锁。悲观锁是将资源锁住,等之前获得锁的线程释放锁之后&am…...
1786_MTALAB代码生成把通用函数生成独立文件
全部学习汇总: GitHub - GreyZhang/g_matlab: MATLAB once used to be my daily tool. After many years when I go back and read my old learning notes I felt maybe I still need it in the future. So, start this repo to keep some of my old learning notes…...
2023/09/19 qt day3
头文件 #ifndef WIDGET_H #define WIDGET_H #include <QWidget> #include <QDebug> #include <QTime> #include <QTimer> #include <QPushButton> #include <QTextEdit> #include <QLineEdit> #include <QLabel> #include &l…...
Docker 学习总结(78)—— Docker Rootless 让你的容器更安全
前言 在以 root 用户身份运行 Docker 会带来一些潜在的危害和安全风险,这些风险包括: 容器逃逸:如果一个容器以 root 权限运行,并且它包含了漏洞或者被攻击者滥用,那么攻击者可能会成功逃出容器,并在宿主系统上执行恶意操作。这会导致宿主系统的安全性受到威胁。 特权升…...
【Python】 -- 趣味代码 - 小恐龙游戏
文章目录 文章目录 00 小恐龙游戏程序设计框架代码结构和功能游戏流程总结01 小恐龙游戏程序设计02 百度网盘地址00 小恐龙游戏程序设计框架 这段代码是一个基于 Pygame 的简易跑酷游戏的完整实现,玩家控制一个角色(龙)躲避障碍物(仙人掌和乌鸦)。以下是代码的详细介绍:…...
FastAPI 教程:从入门到实践
FastAPI 是一个现代、快速(高性能)的 Web 框架,用于构建 API,支持 Python 3.6。它基于标准 Python 类型提示,易于学习且功能强大。以下是一个完整的 FastAPI 入门教程,涵盖从环境搭建到创建并运行一个简单的…...
2.Vue编写一个app
1.src中重要的组成 1.1main.ts // 引入createApp用于创建应用 import { createApp } from "vue"; // 引用App根组件 import App from ./App.vue;createApp(App).mount(#app)1.2 App.vue 其中要写三种标签 <template> <!--html--> </template>…...
【C++从零实现Json-Rpc框架】第六弹 —— 服务端模块划分
一、项目背景回顾 前五弹完成了Json-Rpc协议解析、请求处理、客户端调用等基础模块搭建。 本弹重点聚焦于服务端的模块划分与架构设计,提升代码结构的可维护性与扩展性。 二、服务端模块设计目标 高内聚低耦合:各模块职责清晰,便于独立开发…...
智能AI电话机器人系统的识别能力现状与发展水平
一、引言 随着人工智能技术的飞速发展,AI电话机器人系统已经从简单的自动应答工具演变为具备复杂交互能力的智能助手。这类系统结合了语音识别、自然语言处理、情感计算和机器学习等多项前沿技术,在客户服务、营销推广、信息查询等领域发挥着越来越重要…...
【JVM面试篇】高频八股汇总——类加载和类加载器
目录 1. 讲一下类加载过程? 2. Java创建对象的过程? 3. 对象的生命周期? 4. 类加载器有哪些? 5. 双亲委派模型的作用(好处)? 6. 讲一下类的加载和双亲委派原则? 7. 双亲委派模…...
mac 安装homebrew (nvm 及git)
mac 安装nvm 及git 万恶之源 mac 安装这些东西离不开Xcode。及homebrew 一、先说安装git步骤 通用: 方法一:使用 Homebrew 安装 Git(推荐) 步骤如下:打开终端(Terminal.app) 1.安装 Homebrew…...
论文阅读笔记——Muffin: Testing Deep Learning Libraries via Neural Architecture Fuzzing
Muffin 论文 现有方法 CRADLE 和 LEMON,依赖模型推理阶段输出进行差分测试,但在训练阶段是不可行的,因为训练阶段直到最后才有固定输出,中间过程是不断变化的。API 库覆盖低,因为各个 API 都是在各种具体场景下使用。…...
深入浅出Diffusion模型:从原理到实践的全方位教程
I. 引言:生成式AI的黎明 – Diffusion模型是什么? 近年来,生成式人工智能(Generative AI)领域取得了爆炸性的进展,模型能够根据简单的文本提示创作出逼真的图像、连贯的文本,乃至更多令人惊叹的…...
Cilium动手实验室: 精通之旅---13.Cilium LoadBalancer IPAM and L2 Service Announcement
Cilium动手实验室: 精通之旅---13.Cilium LoadBalancer IPAM and L2 Service Announcement 1. LAB环境2. L2公告策略2.1 部署Death Star2.2 访问服务2.3 部署L2公告策略2.4 服务宣告 3. 可视化 ARP 流量3.1 部署新服务3.2 准备可视化3.3 再次请求 4. 自动IPAM4.1 IPAM Pool4.2 …...
