```java 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());
}
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 student: list){
if (predicate.test(student)) res.add(student);
}
System.out.println(res);
}
} ```
该代码示例展示了一个Java程序,定义了一个静态Predicate,用于检查Student对象的classID是否存在于HashSet中。在main方法中,创建了Student对象列表,并通过遍历并应用Predicate来过滤出满足条件的学生,结果存储在新的列表中并打印出来。

1238

被折叠的 条评论
为什么被折叠?



