You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java中如何过滤对象的列表变量?Student类过滤需求咨询

筛选包含指定课程的Student对象:两种实现方式

嘿,这个需求其实挺典型的,咱们分两种场景来聊,你可以根据自己的情况选:

1. 一次性使用:直接用Lambda表达式(无需自定义Predicate)

如果只是单次筛选,完全不用额外写Predicate,直接在Stream的filter方法里写lambda逻辑就行。核心是判断Student的classes数组里是否包含"math",这里推荐用Arrays.stream()配合anyMatch,比转成List再用contains更高效:

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class Main {
    public static void main(String[] args) {
        Student sa = new Student("John", new String[]{"math", "physics"});
        Student sb = new Student("Jack", new String[]{"math", "english"});
        Student sc = new Student("Maria", new String[]{"math", "chemistry"});
        Student sd = new Student("Tom", new String[]{"english", "history"}); // 测试用的无math学生

        List<Student> students = Arrays.asList(sa, sb, sc, sd);

        // 直接用lambda筛选
        List<Student> mathStudents = students.stream()
                .filter(student -> 
                    // 先判空避免NPE,再检查数组是否包含math
                    student.getClasses() != null && 
                    Arrays.stream(student.getClasses()).anyMatch("math"::equals)
                )
                .collect(Collectors.toList());

        // 输出结果
        mathStudents.forEach(s -> System.out.println(s.getName())); // John, Jack, Maria
    }
}

2. 多次复用:自定义Predicate

如果你的代码里多处需要判断“学生是否包含math课程”,那自定义一个Predicate会让代码更整洁、复用性更强:

import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public class Main {
    // 自定义Predicate,复用判断逻辑
    private static final Predicate<Student> HAS_MATH_CLASS = student -> 
        student.getClasses() != null && 
        Arrays.stream(student.getClasses()).anyMatch("math"::equals);

    public static void main(String[] args) {
        Student sa = new Student("John", new String[]{"math", "physics"});
        Student sb = new Student("Jack", new String[]{"math", "english"});
        Student sc = new Student("Maria", new String[]{"math", "chemistry"});
        Student sd = new Student("Tom", new String[]{"english", "history"});

        List<Student> students = Arrays.asList(sa, sb, sc, sd);

        // 使用自定义Predicate筛选
        List<Student> mathStudents = students.stream()
                .filter(HAS_MATH_CLASS)
                .collect(Collectors.toList());

        mathStudents.forEach(s -> System.out.println(s.getName()));
    }
}

补充小提示

  • 一定要加student.getClasses() != null的判断,不然如果某个Student的classes是null,会直接抛出NullPointerException。
  • 如果需要支持动态课程名(比如不是固定筛选math,而是可以传任意课程),可以把Predicate改成带参数的方法,比如:
    private static Predicate<Student> hasClass(String className) {
        return student -> 
            student.getClasses() != null && 
            Arrays.stream(student.getClasses()).anyMatch(className::equals);
    }
    
    然后用的时候就写filter(hasClass("math"))或者filter(hasClass("english")),灵活性更高。

内容的提问来源于stack exchange,提问作者dexhunter

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:45:56