java_stream_predicate

91 阅读1分钟
public class Main {

    private static final Predicate<Student> predicate;

    static {
        HashSet<Integer> myset = new HashSet<>();
        myset.add(1);
        myset.add(2);
        myset.add(3);
        predicate= s-> myset.contains(s.getClassID()); // s相当于传入的参数
    }

    public static void main(String[] args) {
        Student dd = new Student(2, "dd");
        Student xx = new Student(1, "xx");
        Student yy = new Student(6, "yy");

        List<Student> list = new ArrayList<>();
        list.add(dd);
        list.add(xx);
        list.add(yy);
        
        List<Student> res = new ArrayList<>();
        for (Student s: list){
            if (predicate.test(s)) res.add(student);
        }

        System.out.println(res);
    }
}