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

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

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

一、集合对象定义

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


我的学生类代码如下:

packagecom.uiotsoft.productmanual.controller;
importio.swagger.annotations.ApiModel;
importio.swagger.annotations.ApiModelProperty;
importlombok.AllArgsConstructor;
importlombok.Data;
importlombok.NoArgsConstructor;
importjava.time.LocalDate;
importjava.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@NoArgsConstructorpublicclassStudentimplementsComparable<Student> {
@ApiModelProperty("姓名")
privateStringname;
@ApiModelProperty("性别 true男 false女")
privateBooleansex;
@ApiModelProperty("年龄")
privateIntegerage;
@ApiModelProperty("身高,单位米")
privateDoubleheight;
@ApiModelProperty("出生日期")
privateLocalDatebirthday;
@OverridepublicintcompareTo(Studentstudent) {
returnthis.age.compareTo(student.getAge());
    }
@OverridepublicStringtoString() {
returnString.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 学生信息列表*/publicstaticvoidprintStudentList(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=newArrayList<>();
// 添加测试数据,请不要纠结数据的严谨性studentList.add(newStudent("张三", true, 18, 1.75, LocalDate.of(2005, 3, 26)));
studentList.add(newStudent("李四", false, 16, 1.67, LocalDate.of(2007, 8, 30)));
studentList.add(newStudent("王五", true, 23, 1.89, LocalDate.of(2000, 1, 16)));
studentList.add(newStudent("麻六", false, 27, 1.75, LocalDate.of(1996, 9, 20)));
studentList.add(newStudent("刘七", false, 30, 1.93, LocalDate.of(1993, 6, 19)));
studentList.add(newStudent("王八", 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,为博主原创文章,如果需要转载,请注明出处,谢谢!**


本文完结!

相关文章
|
4月前
|
Java
java8中List对象转另一个List对象
java8中List对象转另一个List对象
222 0
|
4月前
|
消息中间件 NoSQL Java
别再用 Redis List 实现消息队列了,Stream 专为队列而生
别再用 Redis List 实现消息队列了,Stream 专为队列而生
129 0
|
4月前
|
数据处理
利用Stream流将取到的对象List<对象>形式数据进行分组统计转变成Map<分组条件,数量统计>形式
利用Stream流将取到的对象List<对象>形式数据进行分组统计转变成Map<分组条件,数量统计>形式
46 0
|
3月前
|
消息中间件 负载均衡 NoSQL
Redis系列学习文章分享---第七篇(Redis快速入门之消息队列--List实现消息队列 Pubsub实现消息队列 stream的单消费模式 stream的消费者组模式 基于stream消息队列)
Redis系列学习文章分享---第七篇(Redis快速入门之消息队列--List实现消息队列 Pubsub实现消息队列 stream的单消费模式 stream的消费者组模式 基于stream消息队列)
45 0
|
3月前
|
Java
java操作list使用Stream
java操作list使用Stream
|
4月前
|
Java
Java8中List转Map的几种方式
Java8中List转Map的几种方式
98 1
|
4月前
【stream】List根据某个字段求和
【stream】List根据某个字段求和
271 0
|
4月前
|
Java
list集合 使用java8同一列表获取前一条的数据放到当前对象中
list集合 使用java8同一列表获取前一条的数据放到当前对象中
|
4月前
|
Java API
java 对象list 使用stream进行过滤
在Java中,你可以使用Stream API对对象列表进行过滤。假设你有一个`List<MyObject>`,并且你想根据某些条件过滤出特定的对象。以下是一个示例: ```java import java.util.List; import java.util.stream.Collectors; public class Main { public static void main(String[] args) { List<MyObject> myObjects = ... // 初始化你的对象列表 List<MyObject> filter
313 1
如何使用Stream流将List转换为Map
如何使用Stream流将List转换为Map