Java8使用stream().filter()过滤List对象(查找符合条件的对象集合)
2021-04-04 08:27
标签:ring inf col tor 写在前面 测试 循环 ide 操作 使用Java8或更高版本,使用stream().filter()来过滤一个List对象,查找符合条件的对象集合。 如上就是List对象类了。这里稍微的使用了一点链式编程。 过滤要求,我们需要寻找年龄大于等于15的年轻人。 进行年龄大于等于15的过滤操作。 可以看到我们的过滤条件 Java8使用stream().filter()过滤List对象(查找符合条件的对象集合) 标签:ring inf col tor 写在前面 测试 循环 ide 操作 原文地址:https://www.cnblogs.com/cao-1/p/13429946.html写在前面
List对象类
public class StudentInfo {
private String name;
private String gender;
private int age;
private int height;
public StudentInfo setName(String mName) {
name = mName;
return this;
}
public StudentInfo setGender(String mGender) {
gender = mGender;
return this;
}
public StudentInfo setAge(int mAge) {
age = mAge;
return this;
}
public StudentInfo setHeight(int mHeight) {
height = mHeight;
return this;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public String getGender() {
return gender;
}
public int getHeight() {
return height;
}
@Override
public String toString() {
return "StudentInfo{" +
"name=‘" + name + ‘\‘‘ +
", gender=‘" + gender + ‘\‘‘ +
", age=" + age +
", height=" + height +
‘}‘;
}
}
测试数据
public static void main(String[] args) {
List
使用filter()过滤List
List
StudentInfo{name=‘GT Name‘, gender=‘Female‘, age=0, height=180}
StudentInfo{name=‘GT Name‘, gender=‘Male‘, age=1, height=180}
StudentInfo{name=‘GT Name‘, gender=‘Female‘, age=2, height=180}
StudentInfo{name=‘GT Name‘, gender=‘Male‘, age=3, height=180}
StudentInfo{name=‘GT Name‘, gender=‘Female‘, age=4, height=180}
StudentInfo{name=‘GT Name‘, gender=‘Male‘, age=5, height=180}
StudentInfo{name=‘GT Name‘, gender=‘Female‘, age=6, height=180}
StudentInfo{name=‘GT Name‘, gender=‘Male‘, age=7, height=180}
StudentInfo{name=‘GT Name‘, gender=‘Female‘, age=8, height=180}
StudentInfo{name=‘GT Name‘, gender=‘Male‘, age=9, height=180}
StudentInfo{name=‘GT Name‘, gender=‘Female‘, age=10, height=180}
StudentInfo{name=‘GT Name‘, gender=‘Male‘, age=11, height=180}
StudentInfo{name=‘GT Name‘, gender=‘Female‘, age=12, height=180}
StudentInfo{name=‘GT Name‘, gender=‘Male‘, age=13, height=180}
StudentInfo{name=‘GT Name‘, gender=‘Female‘, age=14, height=180}
StudentInfo{name=‘GT Name‘, gender=‘Male‘, age=15, height=180}
StudentInfo{name=‘GT Name‘, gender=‘Female‘, age=16, height=180}
StudentInfo{name=‘GT Name‘, gender=‘Male‘, age=17, height=180}
StudentInfo{name=‘GT Name‘, gender=‘Female‘, age=18, height=180}
StudentInfo{name=‘GT Name‘, gender=‘Male‘, age=19, height=180}
StudentInfo{name=‘GT Name‘, gender=‘Female‘, age=20, height=180}
StudentInfo{name=‘GT Name‘, gender=‘Male‘, age=21, height=180}
StudentInfo{name=‘GT Name‘, gender=‘Female‘, age=22, height=180}
StudentInfo{name=‘GT Name‘, gender=‘Male‘, age=23, height=180}
StudentInfo{name=‘GT Name‘, gender=‘Female‘, age=24, height=180}
StudentInfo{name=‘GT Name‘, gender=‘Male‘, age=25, height=180}
StudentInfo{name=‘GT Name‘, gender=‘Female‘, age=26, height=180}
StudentInfo{name=‘GT Name‘, gender=‘Male‘, age=27, height=180}
StudentInfo{name=‘GT Name‘, gender=‘Female‘, age=28, height=180}
StudentInfo{name=‘GT Name‘, gender=‘Male‘, age=29, height=180}
过滤后数据+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
StudentInfo{name=‘GT Name‘, gender=‘Male‘, age=15, height=180}
StudentInfo{name=‘GT Name‘, gender=‘Female‘, age=16, height=180}
StudentInfo{name=‘GT Name‘, gender=‘Male‘, age=17, height=180}
StudentInfo{name=‘GT Name‘, gender=‘Female‘, age=18, height=180}
StudentInfo{name=‘GT Name‘, gender=‘Male‘, age=19, height=180}
StudentInfo{name=‘GT Name‘, gender=‘Female‘, age=20, height=180}
StudentInfo{name=‘GT Name‘, gender=‘Male‘, age=21, height=180}
StudentInfo{name=‘GT Name‘, gender=‘Female‘, age=22, height=180}
StudentInfo{name=‘GT Name‘, gender=‘Male‘, age=23, height=180}
StudentInfo{name=‘GT Name‘, gender=‘Female‘, age=24, height=180}
StudentInfo{name=‘GT Name‘, gender=‘Male‘, age=25, height=180}
StudentInfo{name=‘GT Name‘, gender=‘Female‘, age=26, height=180}
StudentInfo{name=‘GT Name‘, gender=‘Male‘, age=27, height=180}
StudentInfo{name=‘GT Name‘, gender=‘Female‘, age=28, height=180}
StudentInfo{name=‘GT Name‘, gender=‘Male‘, age=29, height=180}
年龄大于大于15
的过滤操作是有效的。
下一篇:java常用加解密工具类
文章标题:Java8使用stream().filter()过滤List对象(查找符合条件的对象集合)
文章链接:http://soscw.com/index.php/essay/71946.html