JAVA语言-利用java8 stream().map()提取List对象的某一列值及排重
发布日期:2021-06-29 22:26:04 浏览次数:2 分类:技术文章

本文共 895 字,大约阅读时间需要 2 分钟。

文章目录

stream().map()提取List对象的某一列值及排重

//测试数据,请不要纠结数据的严谨性List
studentList = new ArrayList<>();studentList.add(new StudentInfo("李小明",true,18,1.76,LocalDate.of(2001,3,23)));studentList.add(new StudentInfo("张小丽",false,18,1.61,LocalDate.of(2001,6,3)));studentList.add(new StudentInfo("王大朋",true,19,1.82,LocalDate.of(2000,3,11)));studentList.add(new StudentInfo("陈小跑",false,17,1.67,LocalDate.of(2002,10,18)));

提取某一列(以name为例)

//输出ListStudentInfo.printStudents(studentList);//从对象列表中提取一列(以name为例)List
nameList = studentList.stream().map(StudentInfo::getName).collect(Collectors.toList());//提取后输出namenameList.forEach(s-> System.out.println(s));

提取age列并排重(使用distinct()函数)

//提取前输出StudentInfo.printStudents(studentList);//从对象列表中提取age并排重List
ageList = studentList.stream().map(StudentInfo::getAge).distinct().collect(Collectors.toList());ageList.forEach(a-> System.out.println(a));

转载地址:https://docker.blog.csdn.net/article/details/109646474 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Hibernate JPA-exists查询(判断某条记录是否存在)
下一篇:maven assembly打包zip报错 You must set at least one file.

发表评论

最新留言

表示我来过!
[***.240.166.169]2024年04月21日 23时12分30秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章