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

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

本篇主要说明在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;
/**
 * <p>Student此类用于:学生信息实体 </p>
 * <p>@author:hujm</p>
 * <p>@date:2023年01月12日 18:36</p>
 * <p>@remark:注意此处加了Lombok的@Data、@AllArgsConstructor、@NoArgsConstructor注解,所以此类的Getter/Setter/toString/全参构造/无参构造都省略 </p>
 */
@Data
@ApiModel(value = "学生信息实体")
@AllArgsConstructor
@NoArgsConstructor
public class Student implements Comparable<Student> {
    @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());
    }
    /**
     * 打印学生信息的静态方法
     *
     * @param studentList 学生信息列表
     */
    public static void printStudentList(List<Student> 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<Student> 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<Student> ageHeightList = studentList.stream().filter(student -> student.getAge() < 25 && student.getHeight() > 1.7).collect(Collectors.toList());
// 输出符合条件的学生列表
Student.printStudentList(ageHeightList);

结果如下图:

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

本文完结!


相关文章
|
存储 安全 Java
【Java集合类面试二十五】、有哪些线程安全的List?
线程安全的List包括Vector、Collections.SynchronizedList和CopyOnWriteArrayList,其中CopyOnWriteArrayList通过复制底层数组实现写操作,提供了最优的线程安全性能。
|
安全
List集合特有功能
List集合特有功能
113 2
|
11月前
|
安全 Java 程序员
深入Java集合框架:解密List的Fail-Fast与Fail-Safe机制
本文介绍了 Java 中 List 的遍历和删除操作,重点讨论了快速失败(fail-fast)和安全失败(fail-safe)机制。通过普通 for 循环、迭代器和 foreach 循环的对比,详细解释了各种方法的优缺点及适用场景,特别是在多线程环境下的表现。最后推荐了适合高并发场景的 fail-safe 容器,如 CopyOnWriteArrayList 和 ConcurrentHashMap。
227 5
|
11月前
|
存储 分布式计算 NoSQL
大数据-40 Redis 类型集合 string list set sorted hash 指令列表 执行结果 附截图
大数据-40 Redis 类型集合 string list set sorted hash 指令列表 执行结果 附截图
98 3
|
10月前
|
NoSQL Java Redis
List集合按照由小到大排序或者由大到小排序
List集合按照由小到大排序或者由大到小排序
192 0
|
NoSQL Java Redis
List集合按照由小到大排序或者由大到小排序
List集合按照由小到大排序或者由大到小排序
102 3
|
11月前
|
Java 编译器 API
从Java 8到Java 17,这些新特性让你的代码起飞!
【10月更文挑战第10天】在软件开发领域,Java作为一种历史悠久且广泛使用的编程语言,不断进化以适应新的需求和挑战。从Java 8到Java 17,每一次版本更新都带来了诸多新特性和改进,极大地提升了开发效率和代码质量。今天,我们就来一起探讨这些新特性,看看它们是如何让我们的代码“起飞”的。
579 0
|
Java
用JAVA架建List集合为树形结构的代码方法
这段代码定义了一个表示树形结构的 `Node` 类和一个用于构建树形结构的 `TreeController`。`Node` 类包含基本属性如 `id`、`pid`、`name` 和 `type`,以及子节点列表 `children`。`TreeController` 包含初始化节点列表并将其转换为树形结构的方法。通过过滤和分组操作实现树形结构的构建。详情可见:[代码示例链接1](http://www.zidongmutanji.com/zsjx/43551.html),[代码效果参考链接2](https://www.257342.com/sitemap/post.html)。
199 5
|
安全 Java
java线程之List集合并发安全问题及解决方案
java线程之List集合并发安全问题及解决方案
1281 1
|
Java API Apache
怎么在在 Java 中对List进行分区
本文介绍了如何将列表拆分为给定大小的子列表。尽管标准Java集合API未直接支持此功能,但Guava和Apache Commons Collections提供了相关API。
305 1