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

如何实现基于查询对象的Java Stream动态运行时过滤?

动态过滤员工列表需求及实现约束

现有代码

Employee类

public class Employee {
    private String emplId;
    private Name name;
    private String dept;
    private String city;
}

员工列表

List<Employee> empLst = Arrays.asList(
        new Employee("1", "henry", "10", "Boston"),
        new Employee("2", "Foster", "10", "san jose"),
        new Employee("3", "Rick", "10", "sfo"),
        new Employee("4", "Ban", "20", "Boston"),
        new Employee("5", "Zen", "20", "Ale"),
        new Employee("6", "Ken", "30", "sfo")
);

功能需求

实现GetEmplList方法,该方法接收一个Employee对象作为查询条件,仅利用该对象中已设置的属性值对员工列表进行过滤,返回所有匹配的员工实例。示例调用如下:

GetEmplList(new Employee().setCity("Boston")); // 返回员工#1和#4
GetEmplList(new Employee().setCity("Boston").setDept("20")); // 仅返回员工#4
GetEmplList(new Employee().setName("Ken")); // 仅返回员工#6

实现约束

禁止使用编译时硬编码的Filter逻辑,例如以下写法不被允许:

empLst.stream()
     .filter(e -> e.getCity().equalsIgnoreCase("Boston"))
     .forEach(e -> System.out.println(e.getName()));

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.27 15:08:19