基于integerlist过滤对象列表

monwx1rj  于 2021-07-11  发布在  Java
关注(0)|答案(1)|浏览(319)

**结案。**此问题不可复制或由打字错误引起。它目前不接受答案。
**想改进这个问题吗?**更新问题,使其成为堆栈溢出的主题。

上个月关门了。
改进这个问题
我有一节课

class Employee
  String id
  String name

我想根据deptid列表过滤出员工列表,deptid列表是一个整数列表

Employee emp1 = new Employee("1","Ally");
Employee emp2 = new Employee("2","Billy");
ArrayList<Employee> employeeList = Arrays.asList(emp1,emp2);
ArrayList<Integer> ids = Arrays.asList(2);

我写的是

List<Employee> filteredList = employeeList.stream()
  .filter(employee -> ids.contains(employee.getId()))
  .collect(Collectors.toList());

但作为输出,我得到一个空数组。

mi7gmzs6

mi7gmzs61#

首先,你需要改变 subscriptionemployee .
要么把你的身份证改成 Strings 并相应地填充或执行以下操作:

List<Employee> filteredList;
filteredList = employeeList.stream()
     .filter(employee -> ids.contains(Integer.parseInt(employee.getId())))
     .collect(Collectors.toList());

其他选择包括更改 Employee 类来使用 int 作为身份证。

相关问题