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

2021-04-04 08:27

阅读:670

标签:ring   inf   col   tor   写在前面   测试   循环   ide   操作   

写在前面

使用Java8或更高版本,使用stream().filter()来过滤一个List对象,查找符合条件的对象集合。

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 +
                ‘}‘;
    }
}

如上就是List对象类了。这里稍微的使用了一点链式编程。

测试数据

public static void main(String[] args) {
    List lsStudentData = new ArrayList();
    //数据添加
    for (int i = 0; i 

使用filter()过滤List

过滤要求,我们需要寻找年龄大于等于15的年轻人。

List lsYoungStudent = lsStudentData.stream().filter(a -> a.getAge() >= 15).collect(Collectors.toList());
for (StudentInfo studentItem : lsYoungStudent) {
    System.out.println(studentItem.toString());
}

进行年龄大于等于15的过滤操作。

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的过滤操作是有效的。

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

标签:ring   inf   col   tor   写在前面   测试   循环   ide   操作   

原文地址:https://www.cnblogs.com/cao-1/p/13429946.html


评论


亲,登录后才可以留言!