ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2021-02-16 15:02:26  阅读:249  来源: 互联网

标签:name stream gender age List height filter StudentInfo public


 

 

内容简介

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

List对象类(StudentInfo)

public class StudentInfo implements Comparable<StudentInfo> {

//名称
private String name;

//性别 true男 false女
private Boolean gender;

//年龄
private Integer age;

//身高
private Double height;

//出生日期
private LocalDate birthday;

public StudentInfo(String name, Boolean gender, Integer age, Double height, LocalDate birthday){
this.name = name;
this.gender = gender;
this.age = age;
this.height = height;
this.birthday = birthday;
}

public String toString(){
String info = String.format("%s\t\t%s\t\t%s\t\t\t%s\t\t%s",this.name,this.gender.toString(),this.age.toString(),this.height.toString(),birthday.toString());
return info;
}

public static void printStudents(List<StudentInfo> studentInfos){
System.out.println("[姓名]\t\t[性别]\t\t[年龄]\t\t[身高]\t\t[生日]");
System.out.println("----------------------------------------------------------");
studentInfos.forEach(s->System.out.println(s.toString()));
System.out.println(" ");
}

@Override
public int compareTo(StudentInfo ob) {
return this.age.compareTo(ob.getAge());
//return 1;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Boolean getGender() {
return gender;
}

public void setGender(Boolean gender) {
this.gender = gender;
}

public Integer getAge() {
return age;
}

public void setAge(Integer age) {
this.age = age;
}

public Double getHeight() {
return height;
}

public void setHeight(Double height) {
this.height = height;
}

public LocalDate getBirthday() {
return birthday;
}

public void setBirthday(LocalDate birthday) {
this.birthday = birthday;
}
}

StudentInfo对象类

测试数据

复制代码
//测试数据,请不要纠结数据的严谨性
List<StudentInfo> 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)));
复制代码

输出Students列表

 //输出List
StudentInfo.printStudents(studentList);

输出结果如下图:

 使用filter()过滤List

//查找身高在1.8米及以上的男生
List<StudentInfo> boys = studentList.stream().filter(s->s.getGender() && s.getHeight() >= 1.8).collect(Collectors.toList());
//输出查找结果
StudentInfo.printStudents(boys);

结果如下图:

 

标签:name,stream,gender,age,List,height,filter,StudentInfo,public
来源: https://www.cnblogs.com/shoshana-kong/p/14406687.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有