根据Integerlist过滤对象列表 [英] Filter an object list based on Integerlist
本文介绍了根据Integerlist过滤对象列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个类似的课程
class Employee
String id
String name
我想根据作为整数列表的deptId列表过滤出员工列表
I want to filter out the list of employees based on the list of deptId which is an integer list
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());
但是作为输出,我得到了一个空数组.
But as an output I get an empty array.
推荐答案
首先,您需要将subscription
更改为employee
.
First, you need to change subscription
to employee
.
将ID列表更改为Strings
列表并进行相应填充,或执行以下操作:
Either change your ID list to a list of Strings
and populate accordingly or do something like this:
List<Employee> filteredList;
filteredList = employeeList.stream()
.filter(employee -> ids.contains(Integer.parseInt(employee.getId())))
.collect(Collectors.toList());
其他选项包括将您的Employee
类更改为使用int
作为ID.
Other options include changing your Employee
class to use an int
as an id.
这篇关于根据Integerlist过滤对象列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文