Java8 使用 stream().filter()过滤List对象(查找符合条件的对象集合)
内容简介
本文主要说明在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);
结果如下图:

Java8 使用 stream().filter()过滤List对象(查找符合条件的对象集合)的更多相关文章
- 【转载】 C#中通过Where方法查找出所有符合条件的元素集合
在C#的List集合对象中,FirstOrDefault方法可以用于查找List集合中符合条件的第一个元素,如果需要根据条件查找到List集合中的所有符合条件的元素对象集合,则需要使用到List集合的 ...
- 【转载】 C#中List集合使用First方法查找符合条件的第一个元素
在C#的List集合相关操作中,很多时候需要从List集合中查找出符合条件的第一个元素对象,如果确认在List集合中一定存在符合条件的元素,则可以使用First方法来查找,First方法调用格式为Fi ...
- C# ArcgisEngine开发中,对一个图层进行过滤,只显示符合条件的要素
转自原文 C# ArcgisEngine开发中,对一个图层进行过滤,只显示符合条件的要素 有时候,我们要对图层上的地物进行有选择性的显示,以此来满足实际的功能要求. 按下面介绍的方法可轻松实现图层属性 ...
- linux查找符合条件的文件并删除
找到根目录下所有的以test开头的文件并把查找结果当做参数传给rm -rf命令进行删除: 1.find / -name “test*” |xargs rm -rf 2.find / -name “te ...
- 【转】C# ArcgisEngine开发中,对一个图层进行过滤,只显示符合条件的要素
有时候,我们要对图层上的地物进行有选择性的显示,以此来满足实际的功能要求. 按下面介绍的方法可轻松实现图层属性过滤显示: 1.当图层已经加载时 private void ShowByFilter(Ax ...
- JavaScript 数组中查找符合条件的值
数组实例的find方法,用于找出第一个符合条件的数组成员.它的参数是一个回调函数,所有数组成员依次执行该回调函数,直到找出第一个返回值为true的成员,然后返回该成员.如果没有符合条件的成员,则返回u ...
- js删除数组对象中符合条件的数据
var data = [{}, {}, {}, {Id:1}] var datawilldele = [];//2,4,5 data.forEach(function (v, i,arry) { if ...
- js中数组对象根据内容查找符合的第一个对象
今天在写一个混合开发版的app,其中一个功能是扫描快递单号,客户要求不能扫描重复的快递单号!所有就验证查出. 首先实现思路就是: 1.定义一个全局数组变量:var nubList = []; 2.进入 ...
- 【前端】js中数组对象根据内容查找符合的第一个对象
今天在写一个混合开发版的app,其中一个功能是扫描快递单号,客户要求不能扫描重复的快递单号!所有就验证查出. 首先实现思路就是: 1.定义一个全局数组变量:var nubList = []; 2.进入 ...
随机推荐
- Codeforces 463D Gargari and Permutations:隐式图dp【多串LCS】
题目链接:http://codeforces.com/problemset/problem/463/D 题意: 给你k个1到n的排列,问你它们的LCS(最长公共子序列)是多长. 题解: 因为都是1到n ...
- css绝对对齐
方法1:使用text-align:justify 能够兼容所有的浏览器,但是一定要在模块和模块或者字之间存在空格,换行符或者制表符,这样才能起作用 ;;} /* 说明: 1.IE中要实现块内单行两端对 ...
- Linux IO实时监控iostat命令详解(转)
简介 iostat主要用于监控系统设备的IO负载情况,iostat首次运行时显示自系统启动开始的各项统计信息,之后运行iostat将显示自上次运行该命令以后的统计信息.用户可以通过指定统计的次数和时间 ...
- NSDictionary 用法
//Dictionary //不可变 //字典里面:是以键值对存放的 //字典存放顺序是无序的 //字典里面:value可以重复,但key不能重复 //字典里面:key一般用字符串表示,value可用 ...
- SQL SERVER 中的*=和=*
一.* =和= * 是在sql server2000中左连接,右连接的用法相当于left join 和right join,现在sql2005和2008默认是不支持的,可以设置兼容2000或2008 ...
- Python:字典的pop()方法
pop():移除序列中的一个元素(默认最后一个元素),并且返回该元素的值. 一)移除list的元素,若元素序号超出list,报错:pop index out of range(超出范围的流行指数): ...
- js实现翻牌效果
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- canvas线条笔帽及连接
1) 线条笔帽篇: 1 function draw (id) { 2 var canvas = document.getElementById(id); 3 context = canvas.getC ...
- centos7 中文乱码解决方法
centos7 中文乱码解决方法 标签(空格分隔): centos7 1.查看安装中文包: 查看系统是否安装中文语言包 (列出所有可用的公共语言环境的名称,包含有zh_CN) # locale -a ...
- Project Online JS 添加Ribbon按钮
var Projects = Projects || {}; (function () { Projects.ribbonButtonClick = function (name) { var pro ...