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

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流的常用方法(自用)

自用的笔记, 有&#x1f6a9; 需要多看 基本数据 自定义实体 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模型的时候&#xff0c;第一次看见了nn.Embedding函数&#xff0c;以前接触CV比较多&#xff0c;很少学习词嵌入方面的&#xff0c;找了一些资料一开始也不是很理解&#xff0c;多看了两遍后&#xff0c;突然顿悟&#xff0c;特此记录。 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安装包 - 阿里镜像站(镜像站使用方法&#xff1a;查找最新非rc版本的golang安装包) golang安装包 - 中科大镜像站 go二进制文件下载 - 南京大学开源镜像站 Go语言官网(Google中国) Go语言官网(Go团队) 截至目前&#xff08;2023年9月17日&#x…...

2000-2018年各省能源消费和碳排放数据

2000-2018年各省能源消费和碳排放数据 1、时间&#xff1a;2000-2018年 2、范围&#xff1a;30个省市 3、指标&#xff1a;id、year、ENERGY、COAL、碳排放倒数*100 4、来源&#xff1a;能源年鉴 5、指标解释&#xff1a; 2018年碳排放和能源数据为插值法推算得到 碳排放…...

C# ref 学习1

ref 关键字用在四种不同的上下文中&#xff1b; 1.在方法签名和方法调用中&#xff0c;按引用将参数传递给方法。 2.在方法签名中&#xff0c;按引用将值返回给调用方。 3.在成员正文中&#xff0c;指示引用返回值是否作为调用方欲修改的引用被存储在本地&#xff0c;或在一般…...

MQ - 08 基础篇_消费者客户端SDK设计(下)

文章目录 导图Pre概述消费分组协调者消费分区分配策略轮询粘性自定义消费确认确认后删除数据确认后保存消费进度数据消费失败处理从服务端拉取数据失败本地业务数据处理失败提交位点信息失败总结导图 Pre...

Flutter层对于Android 13存储权限的适配问题

感觉很久没有写博客了&#xff0c;不对&#xff0c;的确是很久没有写博客了。原因我不怎么想说&#xff0c;玩物丧志了。后面渐渐要恢复之前的写作节奏。今天来聊聊我最近遇到的一个问题&#xff1a; Android 13版本对于storage权限的控制问题。 我们都知道&#xff0c;Andro…...

Android kotlin开源项目-功能标题目录

目录 一、BRVAH二、开源项目1、RV列表动效&#xff08;标题目录&#xff09;2、拖拽与侧滑&#xff08;标题目录&#xff09;3、数据库&#xff08;标题目录&#xff09;4、树形图(多级菜单)&#xff08;标题目录&#xff09;5、轮播图与头条&#xff08;标题目录&#xff09;6…...

Linux下,基于TCP与UDP协议,不同进程下单线程通信服务器

C语言实现Linux下&#xff0c;基于TCP与UDP协议&#xff0c;不同进程下单线程通信服务器 一、TCP单线程通信服务器 先运行server端&#xff0c;再运行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);// 创建一个定时器&#xff0c;等待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】初识指令(上)

前言&#xff1a; Vue 会根据不同的指令&#xff0c;针对标签实现不同的功能。 在 Vue 中&#xff0c;指定就是带有 v- 前缀 的特殊 标签属性&#xff0c;比如&#xff1a; <div v-htmlstr> </div> 这里问题就来了&#xff0c;既然 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的缩写&#xff0c;即我们所说的比较交换。该操作的作用就是保证数据一致性、操作原子性。 cas是一种基于锁的操作&#xff0c;而且是乐观锁。在java中锁分为乐观锁和悲观锁。悲观锁是将资源锁住&#xff0c;等之前获得锁的线程释放锁之后&am…...

1786_MTALAB代码生成把通用函数生成独立文件

全部学习汇总&#xff1a; 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 权限运行,并且它包含了漏洞或者被攻击者滥用,那么攻击者可能会成功逃出容器,并在宿主系统上执行恶意操作。这会导致宿主系统的安全性受到威胁。 特权升…...

测试微信模版消息推送

进入“开发接口管理”--“公众平台测试账号”&#xff0c;无需申请公众账号、可在测试账号中体验并测试微信公众平台所有高级接口。 获取access_token: 自定义模版消息&#xff1a; 关注测试号&#xff1a;扫二维码关注测试号。 发送模版消息&#xff1a; import requests da…...

java_网络服务相关_gateway_nacos_feign区别联系

1. spring-cloud-starter-gateway 作用&#xff1a;作为微服务架构的网关&#xff0c;统一入口&#xff0c;处理所有外部请求。 核心能力&#xff1a; 路由转发&#xff08;基于路径、服务名等&#xff09;过滤器&#xff08;鉴权、限流、日志、Header 处理&#xff09;支持负…...

模型参数、模型存储精度、参数与显存

模型参数量衡量单位 M&#xff1a;百万&#xff08;Million&#xff09; B&#xff1a;十亿&#xff08;Billion&#xff09; 1 B 1000 M 1B 1000M 1B1000M 参数存储精度 模型参数是固定的&#xff0c;但是一个参数所表示多少字节不一定&#xff0c;需要看这个参数以什么…...

云启出海,智联未来|阿里云网络「企业出海」系列客户沙龙上海站圆满落地

借阿里云中企出海大会的东风&#xff0c;以**「云启出海&#xff0c;智联未来&#xff5c;打造安全可靠的出海云网络引擎」为主题的阿里云企业出海客户沙龙云网络&安全专场于5.28日下午在上海顺利举办&#xff0c;现场吸引了来自携程、小红书、米哈游、哔哩哔哩、波克城市、…...

线程同步:确保多线程程序的安全与高效!

全文目录&#xff1a; 开篇语前序前言第一部分&#xff1a;线程同步的概念与问题1.1 线程同步的概念1.2 线程同步的问题1.3 线程同步的解决方案 第二部分&#xff1a;synchronized关键字的使用2.1 使用 synchronized修饰方法2.2 使用 synchronized修饰代码块 第三部分&#xff…...

关于nvm与node.js

1 安装nvm 安装过程中手动修改 nvm的安装路径&#xff0c; 以及修改 通过nvm安装node后正在使用的node的存放目录【这句话可能难以理解&#xff0c;但接着往下看你就了然了】 2 修改nvm中settings.txt文件配置 nvm安装成功后&#xff0c;通常在该文件中会出现以下配置&…...

STM32F4基本定时器使用和原理详解

STM32F4基本定时器使用和原理详解 前言如何确定定时器挂载在哪条时钟线上配置及使用方法参数配置PrescalerCounter ModeCounter Periodauto-reload preloadTrigger Event Selection 中断配置生成的代码及使用方法初始化代码基本定时器触发DCA或者ADC的代码讲解中断代码定时启动…...

在Ubuntu中设置开机自动运行(sudo)指令的指南

在Ubuntu系统中&#xff0c;有时需要在系统启动时自动执行某些命令&#xff0c;特别是需要 sudo权限的指令。为了实现这一功能&#xff0c;可以使用多种方法&#xff0c;包括编写Systemd服务、配置 rc.local文件或使用 cron任务计划。本文将详细介绍这些方法&#xff0c;并提供…...

零基础设计模式——行为型模式 - 责任链模式

第四部分&#xff1a;行为型模式 - 责任链模式 (Chain of Responsibility Pattern) 欢迎来到行为型模式的学习&#xff01;行为型模式关注对象之间的职责分配、算法封装和对象间的交互。我们将学习的第一个行为型模式是责任链模式。 核心思想&#xff1a;使多个对象都有机会处…...

C++八股 —— 单例模式

文章目录 1. 基本概念2. 设计要点3. 实现方式4. 详解懒汉模式 1. 基本概念 线程安全&#xff08;Thread Safety&#xff09; 线程安全是指在多线程环境下&#xff0c;某个函数、类或代码片段能够被多个线程同时调用时&#xff0c;仍能保证数据的一致性和逻辑的正确性&#xf…...