java使用Stream简单操作集合
效果图
本项目使用springboot
pom依赖
1 2 3 4 5 6 7 8 9 10 11 12 13 | <!--lombok依赖--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version> 1.18 . 16 </version> </dependency> <!--引入junit单元测试依赖--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version> 4.12 </version> </dependency> |
如果还有问题引入
1 2 3 4 5 6 7 8 9 | <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> |
springboot版本
1 2 3 4 5 6 7 | <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version> 2.2 . 1 .RELEASE</version> <!-- <version> 2.3 . 4 .RELEASE</version>--> <relativePath/> </parent> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | package com.java.list; import lombok.Builder; import lombok.Data; /** * @Description: * @Author: Yourheart * @Create: 2022/8/30 12:36 */ @Data @Builder public class ClassRoom { /** * 学生姓名 */ private String name; /** * 学生年龄 */ private Integer age; /** * 学生爱好 */ private String hobby; /** * 学生的特长 */ private String specialSkill; /** * 对于爱好的喜欢程度,01特别喜欢,02一般喜欢,03不喜欢 */ private String status; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | package com.java.list; import org.junit.Test; import java.util.ArrayList; import java.util.Comparator; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; /** * @Description: * @Author: Yourheart * @Create: 2022/8/30 12:35 */ public class CollectTest { @Test public void testList(){ List<ClassRoom> classRoomList= new ArrayList<>(); classRoomList.add(ClassRoom.builder().name( "小红" ).age( 10 ).hobby( "羽毛球" ).specialSkill( null ).status( "01" ).build()); classRoomList.add(ClassRoom.builder().name( "小六子" ).age( 12 ).hobby( "羽毛球,足球" ).specialSkill( null ).status( "01" ).build()); classRoomList.add(ClassRoom.builder().name( "小白" ).age( 10 ).hobby( "篮球" ).specialSkill( "钢琴" ).status( "03" ).build()); classRoomList.add(ClassRoom.builder().name( "小绿" ).age( 12 ).hobby( "羽毛球" ).specialSkill( null ).status( "01" ).build()); classRoomList.add(ClassRoom.builder().name( "小黑" ).age( 12 ).hobby( "乒乓球" ).specialSkill( null ).status( "02" ).build()); classRoomList.add(ClassRoom.builder().name( "小红帽" ).age( 16 ).hobby( "跳绳" ).specialSkill( null ).status( "02" ).build()); classRoomList.add(ClassRoom.builder().name( "小得子" ).age( 12 ).hobby( "羽毛球,篮球" ).specialSkill( null ).build()); //选出对爱好一般喜欢的学生 List<ClassRoom> collect = classRoomList.stream().filter(a -> a.toString().contains( "02" )).collect(Collectors.toList()); System.out.println( "选出对爱好一般喜欢的学生" ); collect.forEach(a->{ System.out.println(a); }); //选出年龄大于12岁的 List<ClassRoom> collect2 = classRoomList.stream().filter(k -> k.getAge() > 12 ).collect(Collectors.toList()); System.out.println( "选出年龄大于12岁的" ); collect2.forEach(k->{ System.out.println(k); }); //选出特长不为空的学生 List<ClassRoom> collect1 = classRoomList.stream().filter(b -> b.getSpecialSkill() != null ).collect(Collectors.toList()); System.out.println( "选出特长不为空的学生" ); collect1.forEach(b->{ System.out.println(b); }); //按照年龄排序 List<ClassRoom> collect3 = classRoomList.stream().sorted(Comparator.comparing(ClassRoom::getAge)).collect(Collectors.toList()); System.out.println( "按照年龄排序" ); collect3.forEach(c->{ System.out.println(c); }); } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异