当前位置: 首页 > 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.hskmm.com/?act=detail&tid=36677

相关文章:

  • 权威调研榜单:湖南张家界旅游团服务TOP3榜单好评深度解析
  • [GXYCTF2019]Ping Ping Ping 1
  • C# 元组 Tuple ValueTuple
  • Java语言的核心特性与大数据应用研究
  • 扣子Coze智能体万字教程:从入门到精通,一文掌握AI工作流搭建 - Leone
  • 轻量服务器Lighthouse + 1Panel + Halo,三步打造你的专属网站
  • (自用)如何使用 mt19937 生成随机数?
  • 第四章 windows实战-向日葵
  • 第四章 windows实战-emlog
  • 第四章 windows实战-wordpress
  • Docling + LangChain + RAG 构建智能文档问答系统
  • 广义串并联图
  • 2025 种植棚/养殖棚/工程/羊肚菌/保温/园林/加厚/绿化/草苫子推荐榜:济宁泽萌草制品 5 星领跑,适配大棚 / 混凝土 / 园艺多场景需求
  • 2025年口碑好的防锈漆厂家最新推荐排行榜
  • 2025 餐饮/电商/品牌策划优选榜:上海物心 5 星领跑,细分领域这些适配型企业值得 pick
  • 2025 灌装/大桶/桶装/纯净/瓶装/水设备精选推荐榜:路得自动化领衔,这些靠谱品牌值得关注
  • 2025 打捆机捆/包装/绕树干/草绳推荐榜:济宁泽萌草制品 5 星领跑,适配农业 / 园艺 / 建筑 / 物流捆扎需求
  • 遇到一例无法保存编辑后的 Excel 文件的问题
  • 告别难找的本地搜图工具!ImageSearch 2.2 携全新 2.0UI 登场
  • 2025 年焊管源头厂家最新推荐榜单:盘点高性能焊管制造企业,助力下游企业精准选品家具/汽车/高精度内刮/镀铝/不锈钢/高强钢焊管厂家推荐
  • 2025 年钢管厂家最新推荐榜:覆盖精密钢管、汽车钢管、高强钢钢管等品类,为下游采购企业提供权威选品参考
  • ubuntu24.04 server 版本安装xfce 使用web novnc 远程桌面
  • 数据结构做题记录
  • 生成函数入门
  • 2025 年 PPT 生成工具品牌最新推荐榜单:AI 备案技术加持 + 千万用户信赖,优质工具全方位测评ppt一键生成/自动生成ppt/ppt制作ai工具推荐
  • 2025 国内西服定制品牌口碑榜:私人/西服/婚礼/高级/高端/高档/男士/女士/轻奢/企业团体职业/企业高管/商务/手工/休闲西服定制厂家推荐
  • 题解:P14023 [ICPC 2024 Nanjing R] 社交媒体
  • 2025 全合成润滑油厂家企业推荐榜:进口润滑油/国产润滑油/国内润滑油/半合成润滑油厂家,技术与服务双驱发展
  • docker安装iotdb
  • ALV 按钮置灰