广告
返回顶部
首页 > 资讯 > 后端开发 > JAVA >【Java基础】Java8 使用 stream().filter()过滤List对象(查找符合条件的对象集合)
  • 816
分享到

【Java基础】Java8 使用 stream().filter()过滤List对象(查找符合条件的对象集合)

java开发语言Poweredby金山文档 2023-09-01 18:09:38 816人浏览 独家记忆
摘要

本篇主要说明在Java8及以上版本中,使用stream().filter()来过滤List对象,查找符合条件的集合。 一、集合对象定义 集合对象以学生类(Student)为例,有学生的基本信息,包括:姓名,性别,年龄,身高,生日几项。

本篇主要说明在Java8及以上版本中,使用stream().filter()来过滤List对象,查找符合条件的集合

一、集合对象定义

集合对象以学生类(Student)为例,有学生的基本信息,包括:姓名,性别,年龄,身高,生日几项。

我的学生类代码如下:

package com.iot.productmanual.controller;import io.swagger.annotations.apiModel;import io.swagger.annotations.ApiModelProperty;import lombok.AllArgsConstructor;import lombok.Data;import lombok.NoArgsConstructor;import java.time.LocalDate;import java.util.List;@Data@ApiModel(value = "学生信息实体")@AllArgsConstructor@NoArgsConstructorpublic class Student implements Comparable {    @ApiModelProperty("姓名")    private String name;    @ApiModelProperty("性别 true男 false女")    private Boolean sex;    @ApiModelProperty("年龄")    private Integer age;    @ApiModelProperty("身高,单位米")    private Double height;    @ApiModelProperty("出生日期")    private LocalDate birthday;    @Override    public int compareTo(Student student) {        return this.age.compareTo(student.getAge());    }    @Override    public String toString() {        return String.fORMat("%s\t\t%s\t\t%s\t\t%s\t\t%s",                this.name, this.sex.toString(), this.age.toString(), this.height.toString(), birthday.toString());    }        public static void printStudentList(List studentList) {        System.out.println("【姓名】\t【性别】\t【年龄】\t\t【身高】\t\t【生日】");        System.out.println("-----------------------------------------------------");        studentList.forEach(s -> System.out.println(s.toString()));        System.out.println(" ");    }}

二、添加测试数据

下面来添加一些测试用的数据,代码如下:

List studentList = new ArrayList<>();// 添加测试数据,请不要纠结数据的严谨性studentList.add(new Student("张三", true, 18, 1.75, LocalDate.of(2005, 3, 26)));studentList.add(new Student("李四", false, 16, 1.67, LocalDate.of(2007, 8, 30)));studentList.add(new Student("王五", true, 23, 1.89, LocalDate.of(2000, 1, 16)));studentList.add(new Student("麻六", false, 27, 1.75, LocalDate.of(1996, 9, 20)));studentList.add(new Student("刘七", false, 30, 1.93, LocalDate.of(1993, 6, 19)));studentList.add(new Student("王八", false, 30, 1.75, LocalDate.of(1993, 6, 19)));

三、使用filter()过滤List

添加过滤条件,比如年龄小于25岁并且身高大于1米7的学生列表

// 输出没有过滤条件的学生列表Student.printStudentList(studentList);// 添加过滤条件,比如年龄小于25岁并且身高大于1米7的学生列表List ageHeightList = studentList.stream().filter(student -> student.getAge() < 25 && student.getHeight() > 1.7).collect(Collectors.toList());// 输出符合条件的学生列表Student.printStudentList(ageHeightList);

结果如下图:

本文首发于CSDN,为博主原创文章,如果需要转载,请注明出处,谢谢!

本文完结!

来源地址:https://blog.csdn.net/weixin_44299027/article/details/128681046

--结束END--

本文标题: 【Java基础】Java8 使用 stream().filter()过滤List对象(查找符合条件的对象集合)

本文链接: https://www.lsjlt.com/news/389239.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作