List collection is filtered by conditions

In the process of project development, we often filter the List collection according to conditions to filter out the results we want or the data that meets the needs of the project. For example: We have a group of students, each student has its own age attribute, but we want to filter out all students in this group of students with age 21 and 22, we need to collect the data in this group of students Filter by conditions to get the results we want.

 

Create a student entity class:

package com.shsj.entity;
public class Student {
private Integer stuNum; // 学号
private String name; // 姓名
private Integer age; // 年龄
public Integer getStuNum() {
    return stuNum;
}
public void setStuNum(Integer stuNum) {
    this.stuNum = stuNum;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public Integer getAge() {
    return age;
}
public void setAge(Integer age) {
    this.age = age;
}
@Override
public String toString() {
    return "Student [stuNum=" + stuNum + ", name=" + name + ", age=" + age + "]";
}
}

Use lambda expression filtering in Java8

package com.shsj.test;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import com.shsj.entity.Student;
/**
* @Description 按条件筛选出List集合中的数据
* @author 刘鹏博
* @version v1.0
* @date 2018年8月23日
*/
public class FilterListTest {
/**
 * @Description 创建四个学生对象并设值,保存在list集合中,模拟数据
 * @author 刘鹏博
 * @return List<Student>
 */
public static List<Student> stuList() {
    List<Student> stuList = new ArrayList<Student>();
    Student stu1 = new Student();
    stu1.setStuNum(20180823);
    stu1.setName("张三");
    stu1.setAge(20);
    stuList.add(stu1);
    Student stu2 = new Student();
    stu2.setStuNum(20180824);
    stu2.setName("李四");
    stu2.setAge(21);
    stuList.add(stu2);
    Student stu3 = new Student();
    stu3.setStuNum(20180825);
    stu3.setName("王二");
    stu3.setAge(21);
    stuList.add(stu3);
    Student stu4 = new Student();
    stu4.setStuNum(20180826);
    stu4.setName("张五");
    stu4.setAge(22);
    stuList.add(stu4);
    return stuList;
}
public static void main(String[] args) {
    List<Student> stuList = stuList();
    // 需要筛选的条件:从stuList中筛选出年龄为21和22的学生
    List<Integer> ageList = new ArrayList<Integer>();
    ageList.add(21);
    ageList.add(22);
    // JDK1.8提供了lambda表达式, 可以从stuList中过滤出符合条件的结果。
    // 定义结果集
    List<Student> result = null;
    result = stuList.stream()
            .filter((Student s) -> ageList.contains(s.getAge()))
            .collect(Collectors.toList());
    // 打印原有stuList集合中的数据
    System.out.println("原有stuList集合中的数据");
    stuList.forEach((Student s) -> System.out.println(s.getName() + "--->" + s.getAge()));
    // 打印过滤筛选后的result结果
    System.out.println("过滤筛选后的result结果");
    result.forEach((Student s) -> System.out.println(s.getName() + "--->" + s.getAge()));
}

 

Guess you like

Origin blog.csdn.net/qq_39999478/article/details/107495454