本篇主要說明在Java8及以上版本中,使用stream().filter()來過濾List對象,查找符合條件的集合。
一、集合對象定義
集合對象以學(xué)生類(Student)為例,有學(xué)生的基本信息,包括:姓名,性別,年齡,身高,生日幾項。
我的學(xué)生類代碼如下:
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此類用于:學(xué)生信息實體 </p>
* <p>@author:hujm</p>
* <p>@date:2023年01月12日 18:36</p>
* <p>@remark:注意此處加了Lombok的@Data、@AllArgsConstructor、@NoArgsConstructor注解,所以此類的Getter/Setter/toString/全參構(gòu)造/無參構(gòu)造都省略 </p>
*/
@Data
@ApiModel(value = "學(xué)生信息實體")
@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());
}
/**
* 打印學(xué)生信息的靜態(tài)方法
*
* @param studentList 學(xué)生信息列表
*/
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(" ");
}
}
二、添加測試數(shù)據(jù)
下面來添加一些測試用的數(shù)據(jù),代碼如下:
List<Student> studentList = new ArrayList<>();
// 添加測試數(shù)據(jù),請不要糾結(jié)數(shù)據(jù)的嚴(yán)謹(jǐn)性
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的學(xué)生列表
// 輸出沒有過濾條件的學(xué)生列表
Student.printStudentList(studentList);
// 添加過濾條件,比如年齡小于25歲并且身高大于1米7的學(xué)生列表
List<Student> ageHeightList = studentList.stream().filter(student -> student.getAge() < 25 && student.getHeight() > 1.7).collect(Collectors.toList());
// 輸出符合條件的學(xué)生列表
Student.printStudentList(ageHeightList);
結(jié)果如下圖:

本文首發(fā)于CSDN,為博主原創(chuàng)文章,如果需要轉(zhuǎn)載,請注明出處,謝謝!文章來源:http://www.zghlxwxcb.cn/news/detail-524699.html
本文完結(jié)!文章來源地址http://www.zghlxwxcb.cn/news/detail-524699.html
到了這里,關(guān)于【Java基礎(chǔ)】Java8 使用 stream().filter()過濾List對象(查找符合條件的對象集合)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!