Java8使用stream().filter()过滤List对象(查找符合条件的对象集合)
写在前面
使用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<StudentInfo> lsStudentData = new ArrayList<>();
//数据添加
for (int i = 0; i < 30; i++) {
StudentInfo student = new StudentInfo();
student.setName("GT Name")
.setAge(i)
.setGender(i % 2 != 0 ? "Male" : "Female")
.setHeight(180);
lsStudentData.add(student);
}
//循环遍历数据
for (StudentInfo studentItem : lsStudentData) {
System.out.println(studentItem.toString());
}
}
使用filter()过滤List
过滤要求,我们需要寻找年龄大于等于15的年轻人。
List<StudentInfo> 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
的过滤操作是有效的。