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

Day21-C:\Users\Lenovo\Desktop\note\code\JavaSE\Basic\src\com\Stream-集合框架(stream)

Stream

JDK开始新增的一套API(java.util.stream),操作集合或者数组的数据

API(Application Programming Interface,应用程序编程接口)

Stream流大量结合了Lambda的语言风格来编程,提供了一种更加强大,更加简单的

package com.Stream;import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;public class StreamTest1 {public static void main(String[] args) {List<String> names = new ArrayList<>();Collections.addAll(names,"张三丰","张无忌","周芷若","赵敏","张强");System.out.println(names);//[张三丰, 张无忌, 周芷若, 赵敏, 张强]//赵找出姓张,且是三个字的名字,存入到一个新集合中去List<String> list = new ArrayList<>();names.forEach(s -> {if(s.startsWith("张")&&s.length()==3){list.add(s);}});System.out.println(list);//开始使用stream流,List<String> list2 = names.stream().filter(s -> s.startsWith("张")).filter(a -> a.length() == 3).collect(Collectors.toList());System.out.println(list2);//filter(s -> s.startsWith("张")).filter(a -> a.length() == 3)可以改成filter(s -> s.startsWith("张")&&s.length() == 3)}
}

Stream使用步骤(流水线)

数据源(集合/数组/...)----->过滤----->排序----->去重----->获取结果(alt+Enter)

image-20251022113233603

package com.Stream;import java.util.*;
import java.util.stream.Stream;public class StreamTest2 {public static void main(String[] args) {//1.如何获取List集合的Stream流List<String> names = new ArrayList<>();Collections.addAll(names,"张三丰","张无忌","周芷若","赵敏","张强");Stream<String> stream = names.stream();//names.stream();alt+Enter//2.如何获取Set集合的Stream流Set<String> set = new HashSet<>();Collections.addAll(set,"刘德华","张曼玉","蜘蛛精","玛德","德玛西亚");Stream<String> stream1 = set.stream();stream1.filter(x->x.contains("德")).forEach(System.out::println);//3.如何获取Map集合的Stream流Map<String, Double> map = new HashMap<>();map.put("古力娜扎",172.3);map.put("迪丽热巴",172.3);map.put("马尔扎哈",172.3);map.put("卡尔扎巴",172.3);//stream()方法是Collections提供的,Map不属于Collections方法,可以先对map集合使用keySet方法,转换为一个set类(Collections类)//map.keySet().stream().forEach(System.out::println);只能处理键属于Set<String> keys = map.keySet();keys.stream().forEach(System.out::println);Collection<Double> values = map.values();Stream<Double> vs = values.stream();Set<Map.Entry<String, Double>> entries = map.entrySet();Stream<Map.Entry<String, Double>> kvs = entries.stream();kvs.filter(e -> e.getKey().equals("巴")).forEach(System.out::println);//4.如何获取的Stream流String[] names2 = {"张翠山","东方不败","唐大山","孤独求败"};Stream<String> s1 = Arrays.stream(names2);//Arrays.stream(names2)或者names.stream()+alt+EnterStream<String> s2 = Stream.of(names2);//Stream.of(names2)}
}

image-20251022132220872

package com.Stream;import com.CollectionandMap.Student;import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Stream;public class StreamTest3 {public static void main(String[] args) {List<Double> scores = new ArrayList<>();Collections.addAll(scores,88.5,100.0,60.0,99.0,9.5,99.6,25.0);//需求1:找出成绩大于60的数据,并升序,再输出scores.stream().filter(s->s>=60).sorted().forEach(System.out::println);List<Student> students = new ArrayList<>();Student s1 =  new Student("蜘蛛精",26,172.5);Student s2 =  new Student("蜘蛛精",26,172.5);Student s3 =  new Student("紫霞",23,167.6);Student s4 =  new Student("白晶晶",25,169.0);Student s5 =  new Student("牛魔王",35,183.3);Student s6 =  new Student("牛夫人",34,168.5);Collections.addAll(students,s1,s2,s3,s4,s5,s6);//需求2:找出年龄大于等于23,且年龄小于等于30岁的学生,并按照年龄降序输出students.stream().filter(s ->s.getAge()>=18 &&s.getAge()<=30).sorted((o1, o2) -> o2.getAge()- o1.getAge())//Lambda表达式调用Comparator比较器方法.forEach(System.out::println);System.out.println("============================");//需求3:取出身高最高的前三名学生,并输出students.stream().sorted((o1, o2) -> Double.compare(o2.getHeight(),o1.getHeight())).limit(3).forEach(System.out::println);//limit返回长度为?的stream流(从前到后数),取前几个数据System.out.println("============================");//需求4:取出身高倒数的两名学生,并输出students.stream().sorted((o1,o2) ->Double.compare(o2.getHeight(),o1.getHeight())).skip(students.size()-2).forEach(System.out::println);//skip是跳过,跳过前面几个的意思System.out.println("============================");//需求5:找出身高超过168的学生叫什么名字,并取出重复名字再输出students.stream().filter(s -> s.getHeight()>=168).map(Student::getName)//map映射方法,本质是将流中的 Student 对象映射为其 name 属性值.distinct() .forEach(System.out::println);//distinct()去重复//(Student::getName)对Student类调用它的getName方法System.out.println("============================");students.stream().filter(s -> s.getHeight()>=168)//这个需要重写Student类中的equals和hashCode方法.distinct() .forEach(System.out::println);//distinct()去重复Stream<String> st1 = Stream.of("张三","李四");Stream<String> st2 = Stream.of("张三2","李四2","王五");Stream<String> allSt = Stream.concat(st1, st2);allSt.forEach(System.out::println);}
}

image-20251022141359042

image-20251022143415067

package com.Stream;import com.CollectionandMap.Student;import java.util.*;
import java.util.stream.Collectors;public class StreamTest4 {public static void main(String[] args) {List<Student> students = new ArrayList<>();Student s1 =  new Student("蜘蛛精",26,172.5);Student s2 =  new Student("蜘蛛精",26,172.5);Student s3 =  new Student("紫霞",23,167.6);Student s4 =  new Student("白晶晶",25,169.0);Student s5 =  new Student("牛魔王",35,183.3);Student s6 =  new Student("牛夫人",34,168.5);Collections.addAll(students,s1,s2,s3,s4,s5,s6);//需求1、请计算出身高超过168的学生有几人long size = students.stream().filter(s -> s.getHeight() >= 168).count();System.out.println(size);//需求2、请找出身高最高的学生对象,并输出//students.stream().sorted((o1, o2) -> Double.compare(o2.getHeight(),o1.getHeight())).limit(1).forEach(System.out::println);Student s = students.stream().max((o1, o2) -> Double.compare(o1.getHeight(), o2.getHeight())).get();System.out.println(s);//需求3、请找出身高最矮的学生对象,并输出Student ss = students.stream().min((o1, o2) -> Double.compare(o1.getHeight(), o2.getHeight())).get();System.out.println(ss);//需求4、请找出身高超过170的学生对象,并放到一个新集合中去返回//Stream流只能收集一次,collect一次List<Student> students1 = students.stream().filter(a -> a.getHeight() >= 170).collect(Collectors.toList());System.out.println(students1);Set<Student> students2 = students.stream().filter(a -> a.getHeight() >= 170).collect(Collectors.toSet());System.out.println(students2);//需求5请找出身高超过170的学生对象,并把学生对象的名字和身高,存入到一个Map集合中返回Map<String, Double> map = students.stream().filter(a -> a.getHeight() >= 170).distinct() .collect(Collectors.toMap(a -> a.getName(), a -> a.getHeight()));System.out.println(map);//Object[] array = students.stream().filter(a -> a.getHeight() >= 170).toArray();//数组中的内容可以重复Student[] array = students.stream().filter(a -> a.getHeight() >= 170).toArray(len->new Student[len]);System.out.println(Arrays.toString(array));}
}
http://www.zskr.cn/news/27498.html

相关文章:

  • 别再手动处理琐事了!用Coze搭建AI工作流,我每天白赚2小时
  • 权威调研榜单:湖南张家界旅游团服务TOP3榜单好评深度解析
  • 权威调研榜单:上海文章批量生成器服务商TOP9榜单技术能力深度解析
  • C# 元组 Tuple ValueTuple
  • Java语言的核心特性与大数据应用研究
  • Dify Windows Docker.desktop 部署
  • 国标GB28181算法算力平台EasyGBS如何在平安乡村搭建无线视频联网监控系统?
  • (自用)如何使用 mt19937 生成随机数?
  • 轻量服务器Lighthouse + 1Panel + Halo,三步打造你的专属网站
  • 企业公用电脑登录安全管控的终极方案:ASP操作系统安全登录管控方案 - 详解
  • 2025 年锂基固化剂生产厂家最新推荐榜单:聚焦优质企业,助力客户精准选品,解决行业选品难题锂基密封/锂基混凝土/厂房锂基固化剂公司推荐
  • 详细介绍:【超全】基于Springboot的海鲜销售管理系统【包括源码+文档+调试】
  • 2025 餐饮/电商/品牌策划优选榜:上海物心 5 星领跑,细分领域这些适配型企业值得 pick
  • 2025 打捆机捆/包装/绕树干/草绳推荐榜:济宁泽萌草制品 5 星领跑,适配农业 / 园艺 / 建筑 / 物流捆扎需求
  • 完整教程:【Python】文件处理
  • 2025/10/21
  • 23423
  • [ACTF2020 新生赛]Exec 1
  • ALV 按钮置灰
  • 2025 年二手中央空调公司最新推荐口碑排行榜:覆盖多场景需求,14000㎡厂房实力企业领衔,助您精准选靠谱商家多联机/柜机二手空调/二手新风/暖通设备公司推荐
  • 微算法科技(NASDAQ MLGO)创建企业级区块链双层共识算法:融合优化DPoS与动态BFT的协同机制设计
  • 深入解析:大模型微调必学教程:从LOSS观测到模型合并导出与部署全流程
  • 题解:P14023 [ICPC 2024 Nanjing R] 社交媒体
  • 2025 全合成润滑油厂家企业推荐榜:进口润滑油/国产润滑油/国内润滑油/半合成润滑油厂家,技术与服务双驱发展
  • 2025年10月微高压氧舱厂家全景解析报告,基于专业测评的技术、性能及市场优势深度分析
  • 2025 年涿州装修公司最新推荐榜,深度解析企业服务能力与市场口碑优势
  • Hive事务管理详解:从ACID原理到UPDATE/DELETE实战 - 实践
  • 详细介绍:【Linux指南】gdb进阶技巧:断点高级玩法与变量跟踪实战
  • 调理neovide之 自定义keymap-不用starter-template的话,直接init.lua中改
  • kettle基本操作4:使用日期字段增量数据同步