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

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流可以非常方便与精简的形式遍历集合&#xff0c;实现过滤、排序等功能 2.将List转成Set stream…...

Python环境搭建

Python|环境搭建&第一个py程序 文章目录 Python|环境搭建&第一个py程序运行环境搭建验证安装是否成功安装PyCharm第一个python程序避免每次打开都进入上次关闭的项目 运行环境搭建 官网&#xff1a;https://www.python.org/downloads/windows/ 注意&#xff1a;下载过…...

JOSEF约瑟 SSJ-41B SSJ-41A 静态时间继电器 延时范围0.02-9.99s

SSJ静态时间继电器 系列型号&#xff1a; SSJ-11A静态时间继电器&#xff1b;SSJ-12A静态时间继电器&#xff1b; SSJ-11B静态时间继电器&#xff1b;SSJ-21B静态时间继电器 SSJ-21A静态时间继电器&#xff1b;SSJ-22A静态时间继电器 SSJ-22B静态时间继电器SSJ-42B静态时间…...

文件MultipartFile上传同时,接收复杂参数

方案一MultipartFile和dto分开 PostMapping("/uploadData") public Result<Object> uploadData(RequestParam("file") MultipartFile file,RequestPart() DataDTO dataDTO) {// 处理文件上传逻辑&#xff0c;如保存文件到本地或云存储// 处理接收到…...

Nginx 获取当前机器IP- Protocol- Port

Full Example Configuration | NGINX Alphabetical index of variables $server_addr&#xff1a;当前nginx所部署的机器 $server_port&#xff1a;当前监听的port server {listen 12345 udp;return ‘$server_addr:$server_port; } 参数说明示例$remote_addr$remote_user$…...

Unity丨自动巡航丨自动寻路丨NPC丨

文章目录 概要功能展示技术细节小结 概要 提示&#xff1a;这里可以添加技术概要 本文功能是制作一个简单的自动巡逻的NPC&#xff0c;随机自动寻路。 功能展示 技术细节 using UnityEngine;public class NPCController : MonoBehaviour {public float moveSpeed 5.0f; // …...

Mysql002:(库和表)操作SQL语句

目录&#xff1a; 》SQL通用规则说明 SQL分类&#xff1a; 》DDL&#xff08;数据定义&#xff1a;用于操作数据库、表、字段&#xff09; 》DML&#xff08;数据编辑&#xff1a;用于对表中的数据进行增删改&#xff09; 》DQL&#xff08;数据查询&#xff1a;用于对表中的数…...

排水管网液位监测,排水管网液位监测方法

排水管网是城市生命线基础设施的重要组成部分&#xff0c;它的正常运行直接关系到城市的洪水防治、环境保护和居民生活质量。对排水管网液位进行实时监测对管理和维护排水系统稳定运行具有重要作用。那么如何对监测排水管网液位呢?本文将着重为大家详细介绍排水管网液位监测方…...

ansible的个人笔记使用记录

1.shell模块使用&#xff0c;shell模块------执行命令&#xff0c;支持特殊符 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 使用大型语言模型来构建你的自定义聊天机器人 在本视频中&#xff0c;你将学习使用OpenAI ChatCompletions格式的组件构建一个机器人。 环境准备 首先&#xff0c;我们将像往常一样设置OpenAI Python包。 import os import openai from dotenv import…...

公司监控员工电脑用什么软件?应该怎么选?

在当今的数字化时代&#xff0c;企业需要对其员工的活动进行适当的监控&#xff0c;以确保企业的信息安全&#xff0c;维护企业的正常运作&#xff0c;并且保证员工的工作效率。然而&#xff0c;如何在尊重员工隐私权的同时&#xff0c;实现这一目标&#xff0c;却是一个挑战。…...

探索创意的新辅助,AI与作家的完美合作

在现代社会&#xff0c;文学创作一直是人类精神活动中的重要一环。从古典文学到现代小说&#xff0c;从诗歌到戏剧&#xff0c;作家们以他们的独特视角和文学天赋为我们展示了丰富多彩的人生世界。而近年来&#xff0c;人工智能技术的快速发展已经渗透到各行各业&#xff0c;文…...

计算机类软件方向适合参加的比赛

前言 博主是一名计算机专业的大三学生&#xff0c;在校时候参加了很多比赛和训练营&#xff0c;现在给大家博主参加过的几个的比赛&#xff0c;希望能给大一大二的学生提供一点建议。 正文 最近也有比赛的&#xff0c;我会从时间线上来给大家推荐一些比赛&#xff0c;并且给…...

win11、win10使用python代码打开和关闭wifi热点的正确方法

问题一 win10、win11&#xff0c;可以在任务栏的WIFI图标启动移动热点&#xff0c;但是无法设置SSID和密码。在网上搜索好久&#xff0c;无解。 万能的网络解决不了&#xff0c;只能自己动手解决了。 问题二 我当前的WiFi驱动程序不支持承载网络&#xff0c;如果我输入netsh…...

spark的数据扩展

会导致数据扩展的操作; 如何避免数据扩展; 一 countDistinct操作 1. 扩展原因 Spark的count distinct操作可能会导致数据扩展的原因是&#xff0c;它需要在执行操作之前对所有不同的值 进行分组。这意味着Spark需要将所有数据加载到内存中&#xff0c;并将其按照不同的值进行…...

前后端分离-图书价格排序案例、后端返回图片地址显示在组件上(打印图片地址)

前后端分离之图书价格排序案例&#xff0c;之后端返回图片地址显示在组件上 注意&#xff1a;分别建前后端项目&#xff0c;前端项目只写前端代码&#xff0c;后端项目只写后端代码1 图书后端 1.1 图书后端之建表 1.2 图书后端之序列化类 1.3 图书后端之视图类 1.4 图书后端之…...

Text-to-SQL小白入门(七)PanGu-Coder2论文——RRTF

论文概述 学习这个RRTF之前&#xff0c;可以先学习一下RLHF。 顺带一提&#xff1a;eosphoros-ai组织「DB-GPT开发者」最新有个新项目Awesome-Text2SQL&#xff1a;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的位置-思维导图 参考来源&#xff1a; 华为培训ppt:HCSCE122_SSL VPN技术 ##SSL的位置 SSL协议套件 ​​​​握手阶段&#xff0c;完成验证&#xff0c;协商出密码套件&#xff0c;进而生成对称密钥&#xff0c;用于后续的加密通信。 加密通信阶段&#xff0c;数据由对…...

M1/M2芯片Parallels Desktop 19安装使用教程(超详细)

引言 在Window上VMware最强&#xff0c;在Mac上毫无疑问Parallels Desktop为最强&#xff01; 今天带来的是最新版Parallels Desktop 19的安装使用教程。 1. 下载安装包 Parallels Desktop 19安装包&#xff1a;https://www.aliyundrive.com/s/ThB8Fs6D3AD Parallels Deskto…...

Zotero中文文献管理终极指南:茉莉花插件一键解决三大痛点

Zotero中文文献管理终极指南&#xff1a;茉莉花插件一键解决三大痛点 【免费下载链接】jasminum A Zotero add-on to retrive CNKI meta data. 一个简单的Zotero 插件&#xff0c;用于识别中文元数据 项目地址: https://gitcode.com/gh_mirrors/ja/jasminum 如果你正在使…...

音乐标签编辑器:让本地音乐元数据管理效率提升90%的开源工具

音乐标签编辑器&#xff1a;让本地音乐元数据管理效率提升90%的开源工具 【免费下载链接】music-tag-web 音乐标签编辑器&#xff0c;可编辑本地音乐文件的元数据&#xff08;Editable local music file metadata.&#xff09; 项目地址: https://gitcode.com/gh_mirrors/mu/…...

PMOS 在电源管理中的高效应用

1. PMOS在高侧开关中的天然优势 我第一次用PMOS做高侧开关是在一个车载设备项目里。当时需要控制12V电源的通断&#xff0c;尝试了几种方案后&#xff0c;发现PMOS简直是这个场景的"天选之子"。相比NMOS&#xff0c;PMOS最大的优势就是控制逻辑简单直接——栅极拉低导…...

TradingAgents-CN智能交易系统:3种部署方案让你5分钟开启AI投资分析

TradingAgents-CN智能交易系统&#xff1a;3种部署方案让你5分钟开启AI投资分析 【免费下载链接】TradingAgents-CN 基于多智能体LLM的中文金融交易框架 - TradingAgents中文增强版 项目地址: https://gitcode.com/GitHub_Trending/tr/TradingAgents-CN 还在为复杂的金融…...

从PolarCTF一道Crypto题,聊聊如何用SageMath秒解自定义群运算的离散对数问题

从PolarCTF一道Crypto题看SageMath在离散对数问题中的实战应用 1. 密码学竞赛中的非标准群运算挑战 在CTF密码学题目中&#xff0c;自定义群运算的离散对数问题&#xff08;DLP&#xff09;是常见的高频考点。近期PolarCTF竞赛中出现了一道典型题目&#xff0c;要求参赛者在非…...

让老旧Mac焕发新生:OpenCore Legacy Patcher完整指南

让老旧Mac焕发新生&#xff1a;OpenCore Legacy Patcher完整指南 【免费下载链接】OpenCore-Legacy-Patcher Experience macOS just like before 项目地址: https://gitcode.com/GitHub_Trending/op/OpenCore-Legacy-Patcher 您的Mac是否被苹果官方"抛弃"&…...

告别格式枷锁:ncmdumpGUI让音乐自由播放变得触手可及

告别格式枷锁&#xff1a;ncmdumpGUI让音乐自由播放变得触手可及 【免费下载链接】ncmdumpGUI C#版本网易云音乐ncm文件格式转换&#xff0c;Windows图形界面版本 项目地址: https://gitcode.com/gh_mirrors/nc/ncmdumpGUI 开篇痛点直击&#xff1a;那些被NCM格式困住的…...

TPAMI 2026 | 跨十大数据集验证,PoundNet重新审视AI图像检测范式

随着 AI 生成图像技术快速演进&#xff0c;伪造内容在网络传播风险持续上升&#xff0c;高鲁棒性检测技术因此成为学界与产业界关注的关键问题。然而&#xff0c;现有不少方法过于追求单一数据集上的短期收益&#xff0c;往往仅围绕“真/假”二分类目标对大规模预训练模型进行专…...

springboot+vue基于web的网上考试系统的设计系统

目录同行可拿货,招校园代理 ,本人源头供货商系统功能模块划分题库管理模块在线考试模块自动阅卷模块技术实现要点扩展功能建议项目技术支持源码获取详细视频演示 &#xff1a;文章底部获取博主联系方式&#xff01;同行可合作同行可拿货,招校园代理 ,本人源头供货商 系统功能模…...

【仅限JDK 25 Early Access用户】:隐藏API `LinkerOptions` 强制启用向量化调用的2行代码,实测吞吐提升2.8倍

第一章&#xff1a;Java 25 外部函数接口优化案例Java 25 正式将外部函数与内存 API&#xff08;Foreign Function & Memory API&#xff09;从预览特性转为正式特性&#xff0c;显著提升了 JVM 与本地代码交互的安全性、性能与开发体验。相比早期 JNI 方案&#xff0c;FFM…...