新闻详情

新闻详情

首页 / 资讯中心 / 详情

JAVA stream常用语法(不定期更新)

发布时间:2026/11/13 6:37:14来源:尧图网络
JAVA stream常用语法(不定期更新)
JAVA stream常用语法常用语法基本数据类型集合操作自定义对象类型集合操作测试类对象实体类总结常用语法基本数据类型集合操作publicstaticvoidmain(String[]args)throwsClassNotFoundException,NoSuchMethodException{setArrayList();setIntArrayList();//获取长度longcountlist.stream().count();System.out.println(countcount);System.out.println(去重);//去重list.stream().distinct().forEach(p-System.out.println((p)));System.out.println(获取前2个);list.stream().limit(2).forEach(p-System.out.println((p)));System.out.println(获取两位数);list.stream().filter(p-p.toString().length()2).forEach(p-System.out.println(p));System.out.println(跳过前四个);list.stream().skip(4).forEach(p-System.out.println((p)));System.out.println(排序);list.stream().sorted(Comparator.comparing(Student::getAge).reversed()).forEach(p-System.out.println(p.getAge()));System.out.println(0~11最小);intaintList.stream().reduce(Integer::min).get();System.out.println(0~11最小a);System.out.println(0~11最大);intbintList.stream().reduce(Integer::max).get();System.out.println(0~11最大b);}publicstaticvoidsetIntArrayList(){for(inti11;i0;i--){intList.add(i);}}publicstaticvoidsetArrayList(){for(inti11;i0;i--){StudentstunewStudent();stu.setAge(i);stu.setName(qwqwi);stu.setNumber(Double.parseDouble(i.i));list.add(stu);}}自定义对象类型集合操作测试类publicstaticvoidmain(String[]args)throwsClassNotFoundException,NoSuchMethodException{//测试转换//获取到传来的json字符串StudentstunewStudent();ListStudentstudentsnewArrayList();stu.setAge(10);stu.setName(name1);stu.setNumber(10.00);students.add(stu);stunewStudent();stu.setAge(11);stu.setName(name2);stu.setNumber(11.00);students.add(stu);stunewStudent();stu.setAge(13);stu.setName(name2);stu.setNumber(13.00);students.add(stu);stunewStudent();stu.setAge(12);stu.setName(name3);stu.setNumber(12.00);students.add(stu);// 返回Map(以名字为Key单个对象为Value如果有多个对象名字相同只将第一次取到的值作为Value)MapString,StudentstudentMapstudents.stream().collect(Collectors.toMap(Student::getName,item-item,(v1,v2)-v1));System.out.println(---- 名字分组结果,单一studentMap);// 返回Map(以名字为Keynumber累加为Value如果有多个对象名字相同将每个对象的number累加)MapString,IntegerstudentNameNumberMapstudents.stream().collect(Collectors.toMap(Student::getName,Student::getAge,(v1,v2)-v1v2));System.out.println(---- 名字分组结果,value累加studentNameNumberMap);// 返回对象列表中指定属性的list列表ListIntegerageListstudents.stream().map(Student::getAge).collect(Collectors.toList());// 年龄列表[10, 11, 13, 12]System.out.println(年龄列表ageList);// 返回Map(以名称为Key集合对象为Value)MapString,ListStudentmapstudents.stream().collect(Collectors.groupingBy(Student::getName));// 分组结果{name3[Student{namename3, age12, number12.0}], name2[Student{namename2, age11, number11.0}, Student{namename2, age13, number13.0}], name1[Student{namename1, age10, number10.0}]}System.out.println(分组结果map);// 返回Map(以年龄为Key名称为Value)MapInteger,Stringmap2students.stream().collect(Collectors.toMap(Student::getAge,Student::getName));// 分组结果2{10name1, 11name2, 12name3, 13name2}System.out.println(分组结果2map2);//返回Map(以年龄为Key对象为Value)MapInteger,Studentmap3students.stream().collect(Collectors.toMap(Student::getAge,item-item));// 分组结果3{10Student{namename1, age10, number10.0}, 11Student{namename2, age11, number11.0}, 12Student{namename3, age12, number12.0}, 13Student{namename2, age13, number13.0}}System.out.println(分组结果3map3);finalbooleanisBigTenstudents.stream().allMatch(p-p.getAge()10);// 年龄是否都大于10falseSystem.out.println(年龄是否都大于10isBigTen);// 求年龄之和LongSummaryStatisticssummaryObjectstudents.stream().collect(Collectors.summarizingLong(Student::getAge));// 年龄之和LongSummaryStatistics{count4, sum46, min10, average11.500000, max13}System.out.println(年龄之和summaryObject);OptionalIntegersumOpstudents.stream().map(Student::getAge).reduce(Integer::sum);// 年龄总和Optional[46]System.out.println(年龄总和sumOp);OptionalIntegerminOpstudents.stream().map(Student::getAge).reduce(Integer::min);// 最小年龄Optional[10]System.out.println(最小年龄minOp);OptionalIntegermaxOpstudents.stream().map(Student::getAge).reduce(Integer::max);IntegeropopmaxOp.orElse(0);// 最大年龄13System.out.println(最大年龄opop);// 计算平均值finalDoubledoubleAveragestudents.stream().collect(Collectors.averagingDouble(p-p.getNumber()));// 金额平均值11.5System.out.println(金额平均值doubleAverage);finalDoubleintAveragestudents.stream().collect(Collectors.averagingInt(p-p.getAge()));// 年龄平均值11.5System.out.println(年龄平均值intAverage);ListStudentlalstudents.stream().map((p)-{StudentstnewStudent();st.setName(namep.getAge());returnst;}).distinct().collect(Collectors.toList());// 去重后的名字年龄数组[Student{namename10, age0, number0.0}, Student{namename11, age0, number0.0}, Student{namename13, age0, number0.0}, Student{namename12, age0, number0.0}]System.out.println(去重后的名字年龄数组lal.toString());ListStudentqqwstudents.stream().map((p)-{p.setName(p.getName().toUpperCase());returnp;}).collect(Collectors.toList());// 去重后的名字大写数组[Student{nameNAME1, age10, number10.0}, Student{nameNAME2, age11, number11.0}, Student{nameNAME2, age13, number13.0}, Student{nameNAME3, age12, number12.0}]System.out.println(去重后的名字大写数组qqw);}对象实体类publicclassStudent{publicStringname;publicintage;publicdoublenumber;publicStringgetName(){returnname;}publicvoidsetName(Stringname){this.namename;}publicintgetAge(){returnage;}publicvoidsetAge(intage){this.ageage;}publicdoublegetNumber(){returnnumber;}publicvoidsetNumber(doublenumber){this.numbernumber;}OverridepublicStringtoString(){returnStudent{namename\, ageage, numbernumber};}}总结通过个人对stream和for的性能比较stream性能一直较差但是相差不大。对于简洁逻辑的集合操作stream操作代码量比for循环代码量简洁。stream语法较复杂易读性较差简单的集合元素操作逻辑推荐使用stream操作但是如果操作逻辑复杂还是推荐使用for操作易读好维护。
网站建设高端定制企业官网
RELATED

相关资讯

更多精彩内容,欢迎继续阅读

较早相关资讯

最新相关资讯

【外设】之大彩串口显示屏 2026/11/13 0:43:49

【外设】之大彩串口显示屏

大彩串口屏初步使用 1 .官网下载 STM32 屏幕 GUI 设计资料 http://www.gz-dc.com/category/typeid/4112 找到 STM32 Keil 工程,移植相关代码因项目而异进行移植,由于项目简单,本人只对用到的指令接口进行修改。 比如:注意事项&…

阅读更多 →
无法下载Windows系统iso文件 2026/11/12 23:59:09

无法下载Windows系统iso文件

当我遇到这个问题的时候,我打开了一个网站: 登录 然后我打算下载的时候: 突然那个官方的连接就可以下载了:

阅读更多 →
【清华代码熊】DeepSeek V4.1 Flash 后训练详解 2026/11/12 9:50:02

【清华代码熊】DeepSeek V4.1 Flash 后训练详解

📌 上期解析了 DeepSeek V4.1 Flash 模型架构改进,本期解析 DeepSeek V4.1 Flash 预训练/后训练技术: 🌟 预训练:45T 文本 多模态混合语料、直接训练 sparse attention(取消 DeepSeek V4 的 dense 冷启动&…

阅读更多 →
Shuffle-R1: Efficient RL framework for Multimodal Large Language Models via Data-centric Dynamic ... 2026/11/12 9:25:38

Shuffle-R1: Efficient RL framework for Multimodal Large Language Models via Data-centric Dynamic ...

文章主要内容和创新点 主要内容 本文聚焦于多模态大语言模型(MLLM)强化学习(RL)训练中的效率问题,提出了一个名为Shuffle-R1的框架。研究发现,当前RL训练存在两个关键缺陷: 优势值坍缩(Advantage Collapsing):批次中大多数优势值集中在零附近,导致有效梯度信号被淹…

阅读更多 →
PRvL: Quantifying the Capabilities and Risks of Large Language Models for PII Redaction 2026/11/12 15:57:44

PRvL: Quantifying the Capabilities and Risks of Large Language Models for PII Redaction

一、文章主要内容总结 本文聚焦于利用大型语言模型(LLMs)实现个人身份信息(PII)脱敏的研究,旨在解决传统脱敏方法(如基于规则的系统、领域特定命名实体识别(NER)模型)泛化能力差、跨格式/跨语境适应性弱的问题。 研究通过全面评估多种LLM架构(包括密集型LLM(D-LLM…

阅读更多 →
LLaVA-RE: Binary Image-Text Relevancy Evaluation with Multimodal Large Language Model 2026/11/13 0:02:27

LLaVA-RE: Binary Image-Text Relevancy Evaluation with Multimodal Large Language Model

文章主要内容和创新点 主要内容 本文聚焦于二进制图像-文本相关性评估任务(判断图像与文本“相关”或“不相关”),针对该任务中文本格式多样、相关性定义随场景变化等挑战,提出了基于多模态大语言模型(MLLM)的解决方案LLaVA-RE。 模型设计:LLaVA-RE基于LLaVA 1.5架构,…

阅读更多 →

今日资讯

本周资讯

本月资讯

看完文章仍有疑问?

联系尧图顾问,获取一对一建站咨询

立即免费咨询 📞 400-888-8888
📞 ✉