Java高级-stream流
stream流
- 1.介绍
- 2.将List转成Set
- 3.将List转成Map
- 4.计算求和reduce
- 5.查找最大值max和最小值min
- 6.Match匹配
- 7.过滤器 filter
- 8.分页limit 跳过skip
- 9.数据排序 sorted
1.介绍
stream流可以非常方便与精简的形式遍历集合,实现过滤、排序等功能
2.将List转成Set
stream.collect(Collectors.toSet());
public class Test01 {public static void main(String[] args) {ArrayList<UserEntity> userEntities = new ArrayList<>();userEntities.add(new UserEntity("张三",16));userEntities.add(new UserEntity("李四", 51));userEntities.add(new UserEntity("王五", 73));userEntities.add(new UserEntity("赵六", 12));userEntities.add(new UserEntity("赵六", 12));// 创建stream有两种方式// 1.串行流// 2.并行流// 并行流 比 串行流 效率要高Stream<UserEntity> stream = userEntities.stream();// 转换成set集合Set<UserEntity> userEntitySet = stream.collect(Collectors.toSet());userEntitySet.forEach(userEntity -> {System.out.println(userEntity);});}
}
3.将List转成Map
需要指定谁作为key,谁作为value
stream.collect(Collectors.toMap());
public class Test02 {public static void main(String[] args) {ArrayList<UserEntity> userEntities = new ArrayList<>();userEntities.add(new UserEntity("张三",16));userEntities.add(new UserEntity("李四", 51));userEntities.add(new UserEntity("王五", 73));userEntities.add(new UserEntity("赵六", 12));// list集合并没有key// 指定userName作为key,user对象作为valueStream<UserEntity> userEntityStream = userEntities.stream();/*** new Function<UserEntity(List集合中的类型), String(Map中的Key)>()*/Map<String, UserEntity> collect = userEntityStream.collect(Collectors.toMap(new Function<UserEntity, String>() {@Overridepublic String apply(UserEntity userEntity) {// 设置key的值return userEntity.getUserName();}}, new Function<UserEntity, UserEntity>() {@Overridepublic UserEntity apply(UserEntity userEntity) {// 设置value的值return userEntity;}}));collect.forEach(new BiConsumer() {@Overridepublic void accept(Object key, Object value) {System.out.println(key + ": " + value);}});}
}
4.计算求和reduce
public class Test03 {public static void main(String[] args) {Stream<Integer> integerStream = Stream.of(10, 89, 204, 56);Optional<Integer> reduce = integerStream.reduce(new BinaryOperator<Integer>() {@Overridepublic Integer apply(Integer integer, Integer integer2) {return integer + integer2;}});System.out.println(reduce.get());System.out.println("================================================");// 计算每个user的年龄的总数ArrayList<UserEntity> userEntities = new ArrayList<>();userEntities.add(new UserEntity("张三",16));userEntities.add(new UserEntity("李四", 51));userEntities.add(new UserEntity("王五", 73));userEntities.add(new UserEntity("赵六", 12));userEntities.add(new UserEntity("赵六", 12));Stream<UserEntity> userEntityStream = userEntities.stream();Optional<UserEntity> sum = userEntityStream.reduce(new BinaryOperator<UserEntity>() {@Overridepublic UserEntity apply(UserEntity u1, UserEntity u2) {return new UserEntity("sum", u1.getAge() + u2.getAge());}});System.out.println(sum.get().getAge());}
}
5.查找最大值max和最小值min
public class Test04 {public static void main(String[] args) {ArrayList<UserEntity> userEntities = new ArrayList<>();userEntities.add(new UserEntity("张三",16));userEntities.add(new UserEntity("李四", 51));userEntities.add(new UserEntity("王五", 73));userEntities.add(new UserEntity("赵六", 12));userEntities.add(new UserEntity("赵六", 12));// 最大值Stream<UserEntity> userEntityStream1 = userEntities.stream();Optional<UserEntity> max = userEntityStream1.max((o1, o2) -> o1.getAge() - o2.getAge());System.out.println("max = " + max.get());// 最小值Stream<UserEntity> userEntityStream2 = userEntities.stream();Optional<UserEntity> min = userEntityStream2.min((o1, o2) -> o1.getAge() - o2.getAge());System.out.println("min = " + min.get());}
}
6.Match匹配
anyMatch:判断条件里,任意一个元素成功,返回true
AllMatch:判断条件里,所有的都是成功,返回true
noneMatch跟allMatch相反,判断条件里的元素,所有的都不是,返回true
public class Test05 {public static void main(String[] args) {ArrayList<UserEntity> userEntities = new ArrayList<>();userEntities.add(new UserEntity("zhangsan",16));userEntities.add(new UserEntity("lisi", 51));userEntities.add(new UserEntity("wangwu", 73));userEntities.add(new UserEntity("zhaoliu", 12));userEntities.add(new UserEntity("zhaoliu", 12));Stream<UserEntity> userEntityStream = userEntities.stream();boolean b = userEntityStream.noneMatch(new Predicate<UserEntity>() {@Overridepublic boolean test(UserEntity userEntity) {return "zhangsan".equals(userEntity.getUserName());}});System.out.println("b = " + b);}
}
7.过滤器 filter
public class Test06 {public static void main(String[] args) {ArrayList<UserEntity> userEntities = new ArrayList<>();userEntities.add(new UserEntity("zhangsan",26));userEntities.add(new UserEntity("lisi", 51));userEntities.add(new UserEntity("wangwu", 73));userEntities.add(new UserEntity("zhaoliu", 12));userEntities.add(new UserEntity("zhaoliu", 63));Stream<UserEntity> userEntityStream = userEntities.stream();userEntityStream.filter(new Predicate<UserEntity>() {@Overridepublic boolean test(UserEntity userEntity) {return "zhaoliu".equals(userEntity.getUserName()) && userEntity.getAge() > 2;}}).forEach(userEntity -> System.out.println(userEntity));}
}
8.分页limit 跳过skip
public class Test07 {public static void main(String[] args) {ArrayList<UserEntity> userEntities = new ArrayList<>();userEntities.add(new UserEntity("zhangsan",26));userEntities.add(new UserEntity("lisi", 51));userEntities.add(new UserEntity("wangwu", 73));userEntities.add(new UserEntity("zhaoliu", 12));userEntities.add(new UserEntity("zhaoliu", 63));// 只查询前5条Stream<UserEntity> userEntityStream1 = userEntities.stream();userEntityStream1.limit(3).forEach(userEntity -> System.out.println(userEntity));System.out.println("====================");// 查询 第2条-4条Stream<UserEntity> userEntityStream2 = userEntities.stream();userEntityStream2.skip(2).limit(2).forEach(userEntity -> System.out.println(userEntity));}
}
9.数据排序 sorted
public class Test08 {public static void main(String[] args) {ArrayList<UserEntity> userEntities = new ArrayList<>();userEntities.add(new UserEntity("zhangsan",26));userEntities.add(new UserEntity("lisi", 51));userEntities.add(new UserEntity("wangwu", 73));userEntities.add(new UserEntity("zhaoliu", 12));userEntities.add(new UserEntity("zhaoliu", 63));Stream<UserEntity> userEntityStream = userEntities.stream();userEntityStream.sorted(new Comparator<UserEntity>() {@Overridepublic int compare(UserEntity o1, UserEntity o2) {return o2.getAge() - o1.getAge();}}).forEach(userEntity -> System.out.println(userEntity));}
}
相关文章:
Java高级-stream流
stream流 1.介绍2.将List转成Set3.将List转成Map4.计算求和reduce5.查找最大值max和最小值min6.Match匹配7.过滤器 filter8.分页limit 跳过skip9.数据排序 sorted 1.介绍 stream流可以非常方便与精简的形式遍历集合,实现过滤、排序等功能 2.将List转成Set stream…...
Python环境搭建
Python|环境搭建&第一个py程序 文章目录 Python|环境搭建&第一个py程序运行环境搭建验证安装是否成功安装PyCharm第一个python程序避免每次打开都进入上次关闭的项目 运行环境搭建 官网:https://www.python.org/downloads/windows/ 注意:下载过…...
JOSEF约瑟 SSJ-41B SSJ-41A 静态时间继电器 延时范围0.02-9.99s
SSJ静态时间继电器 系列型号: SSJ-11A静态时间继电器;SSJ-12A静态时间继电器; SSJ-11B静态时间继电器;SSJ-21B静态时间继电器 SSJ-21A静态时间继电器;SSJ-22A静态时间继电器 SSJ-22B静态时间继电器SSJ-42B静态时间…...
文件MultipartFile上传同时,接收复杂参数
方案一MultipartFile和dto分开 PostMapping("/uploadData") public Result<Object> uploadData(RequestParam("file") MultipartFile file,RequestPart() DataDTO dataDTO) {// 处理文件上传逻辑,如保存文件到本地或云存储// 处理接收到…...
Nginx 获取当前机器IP- Protocol- Port
Full Example Configuration | NGINX Alphabetical index of variables $server_addr:当前nginx所部署的机器 $server_port:当前监听的port server {listen 12345 udp;return ‘$server_addr:$server_port; } 参数说明示例$remote_addr$remote_user$…...
Unity丨自动巡航丨自动寻路丨NPC丨
文章目录 概要功能展示技术细节小结 概要 提示:这里可以添加技术概要 本文功能是制作一个简单的自动巡逻的NPC,随机自动寻路。 功能展示 技术细节 using UnityEngine;public class NPCController : MonoBehaviour {public float moveSpeed 5.0f; // …...
Mysql002:(库和表)操作SQL语句
目录: 》SQL通用规则说明 SQL分类: 》DDL(数据定义:用于操作数据库、表、字段) 》DML(数据编辑:用于对表中的数据进行增删改) 》DQL(数据查询:用于对表中的数…...
排水管网液位监测,排水管网液位监测方法
排水管网是城市生命线基础设施的重要组成部分,它的正常运行直接关系到城市的洪水防治、环境保护和居民生活质量。对排水管网液位进行实时监测对管理和维护排水系统稳定运行具有重要作用。那么如何对监测排水管网液位呢?本文将着重为大家详细介绍排水管网液位监测方…...
ansible的个人笔记使用记录
1.shell模块使用,shell模块------执行命令,支持特殊符 ansible all -m shell -a yum -y install nginx ansible all -m shell -a systemctl restart nginx ansible all -m shell -a systemctl stop nginx && yum -y remove nginx2. file模块…...
OpenAI官方吴达恩《ChatGPT Prompt Engineering 提示词工程师》(7)聊天机器人 / ChatBot
聊天机器人 / ChatBot 使用大型语言模型来构建你的自定义聊天机器人 在本视频中,你将学习使用OpenAI ChatCompletions格式的组件构建一个机器人。 环境准备 首先,我们将像往常一样设置OpenAI Python包。 import os import openai from dotenv import…...
公司监控员工电脑用什么软件?应该怎么选?
在当今的数字化时代,企业需要对其员工的活动进行适当的监控,以确保企业的信息安全,维护企业的正常运作,并且保证员工的工作效率。然而,如何在尊重员工隐私权的同时,实现这一目标,却是一个挑战。…...
探索创意的新辅助,AI与作家的完美合作
在现代社会,文学创作一直是人类精神活动中的重要一环。从古典文学到现代小说,从诗歌到戏剧,作家们以他们的独特视角和文学天赋为我们展示了丰富多彩的人生世界。而近年来,人工智能技术的快速发展已经渗透到各行各业,文…...
计算机类软件方向适合参加的比赛
前言 博主是一名计算机专业的大三学生,在校时候参加了很多比赛和训练营,现在给大家博主参加过的几个的比赛,希望能给大一大二的学生提供一点建议。 正文 最近也有比赛的,我会从时间线上来给大家推荐一些比赛,并且给…...
win11、win10使用python代码打开和关闭wifi热点的正确方法
问题一 win10、win11,可以在任务栏的WIFI图标启动移动热点,但是无法设置SSID和密码。在网上搜索好久,无解。 万能的网络解决不了,只能自己动手解决了。 问题二 我当前的WiFi驱动程序不支持承载网络,如果我输入netsh…...
spark的数据扩展
会导致数据扩展的操作; 如何避免数据扩展; 一 countDistinct操作 1. 扩展原因 Spark的count distinct操作可能会导致数据扩展的原因是,它需要在执行操作之前对所有不同的值 进行分组。这意味着Spark需要将所有数据加载到内存中,并将其按照不同的值进行…...
前后端分离-图书价格排序案例、后端返回图片地址显示在组件上(打印图片地址)
前后端分离之图书价格排序案例,之后端返回图片地址显示在组件上 注意:分别建前后端项目,前端项目只写前端代码,后端项目只写后端代码1 图书后端 1.1 图书后端之建表 1.2 图书后端之序列化类 1.3 图书后端之视图类 1.4 图书后端之…...
Text-to-SQL小白入门(七)PanGu-Coder2论文——RRTF
论文概述 学习这个RRTF之前,可以先学习一下RLHF。 顺带一提:eosphoros-ai组织「DB-GPT开发者」最新有个新项目Awesome-Text2SQL:GitHub - eosphoros-ai/Awesome-Text2SQL: Curated tutorials and resources for Large Language Models, Text2…...
C语言中常见的面试题
解释C语言中的基本数据类型,并举例说明它们的用法和限制。描述C语言中的变量和常量的定义方法,以及它们的作用和区别。解释C语言中的数组和字符串,并说明它们的定义方法和使用注意事项。描述C语言中的循环结构和控制语句,并举例说明它们的用法和限制。解释C语言中的函数和函…...
协议-SSL协议-基础概念01-SSL位置-协议套件-握手和加密过程-对比ipsec
SSL的位置-思维导图 参考来源: 华为培训ppt:HCSCE122_SSL VPN技术 ##SSL的位置 SSL协议套件 握手阶段,完成验证,协商出密码套件,进而生成对称密钥,用于后续的加密通信。 加密通信阶段,数据由对…...
M1/M2芯片Parallels Desktop 19安装使用教程(超详细)
引言 在Window上VMware最强,在Mac上毫无疑问Parallels Desktop为最强! 今天带来的是最新版Parallels Desktop 19的安装使用教程。 1. 下载安装包 Parallels Desktop 19安装包:https://www.aliyundrive.com/s/ThB8Fs6D3AD Parallels Deskto…...
图像处理和深度学习笔记[特殊字符](一)
AI生命周期:数据准备 → 模型训练 → 模型转换 → 部署 → 监控↑ 算法工程师关注 ↑ ↓ 你将专注于此 ↓机器学习开发流程数据收集数据预处理特征提取 数据预处理和 特征提取(其实就是数据清洗和转换) 比较耗时耗力清洗和特征工程模型构…...
5个认知重构,收割你的补偿性Offer
春招反杀指南当别人还在为秋招失利懊悔时,聪明人已经完成了思维系统的彻底升级秋招的硝烟尚未散尽,春招的号角已经吹响。这不是简单的“第二轮机会”,而是认知层面的降维打击战。那些在秋招中凭借简历光环轻松通关的路径已然失效,…...
Phi-4-mini-reasoning应对软件测试:自动生成测试用例与缺陷分析
Phi-4-mini-reasoning应对软件测试:自动生成测试用例与缺陷分析 1. 引言:软件测试的痛点与AI解决方案 在软件开发的生命周期中,测试环节往往占据30%-50%的项目时间。传统测试工作面临两大核心挑战:一是测试用例设计需要大量人工…...
科研人必备:用浏览器插件给IEEEXplore做个‘小手术’,告别20秒加载
科研效率革命:用浏览器插件精准优化IEEEXplore访问体验 每次打开IEEEXplore文献库,那个转不停的加载图标是否让你焦躁不安?作为每天要与学术数据库打交道的科研工作者,20秒的等待时间足以打断思考流,降低工作效率。这背…...
从NDVI到地表温度:用ENVI Band Math一次性搞定植被与热环境分析
ENVI波段运算实战:NDVI与地表温度的高效批量处理技巧 遥感影像分析中,植被指数和地表温度是最基础却又最关键的指标。传统操作流程往往需要反复切换不同工具模块,既耗时又容易出错。而ENVI的Band Math功能就像一把瑞士军刀,能将这…...
FPGA密码锁设计避坑指南:状态机划分、时序约束与安全逻辑的那些事儿
FPGA密码锁设计避坑指南:状态机划分、时序约束与安全逻辑的那些事儿 在FPGA开发领域,密码锁设计看似简单,实则暗藏玄机。许多工程师在完成基础功能后,往往会在状态机划分、时序约束和安全逻辑等环节踩坑。本文将结合实战经验&…...
CH340/CH341安卓USB主机模式开发实战
1. CH340/CH341安卓USB主机模式开发入门 很多开发者第一次接触安卓USB主机模式开发时,都会遇到一个典型问题:为什么我的手机连上CH340模块后毫无反应?这通常是因为安卓设备默认工作在从机模式(USB Device Mode),而连接串口设备需要…...
MiniCPM-V-2_6嵌入式AI应用实战:STM32F103C8T6边缘推理集成
MiniCPM-V-2_6嵌入式AI应用实战:STM32F103C8T6边缘推理集成 最近几年,AI模型越来越“小”,开始往各种硬件设备里钻。你可能听说过在手机、树莓派上跑AI,但有没有想过,在一块只有指甲盖大小、主频72MHz、内存才20KB的S…...
BetterNCM Installer插件管理器:网易云音乐用户的功能扩展工具
BetterNCM Installer插件管理器:网易云音乐用户的功能扩展工具 【免费下载链接】BetterNCM-Installer 一键安装 Better 系软件 项目地址: https://gitcode.com/gh_mirrors/be/BetterNCM-Installer BetterNCM Installer是面向网易云音乐PC用户的插件管理工具&…...
CLIP-GmP-ViT-L-14工具实测:如何用图文匹配优化电商搜索与内容审核
CLIP-GmP-ViT-L-14工具实测:如何用图文匹配优化电商搜索与内容审核 1. 图文匹配技术的商业价值 在数字化商业环境中,图片和文字是两种最核心的内容载体。但长期以来,计算机系统很难真正理解两者之间的语义关联。CLIP-GmP-ViT-L-14模型的出现…...
