[英]Using Streams, see if a list contains a property of an object from another list
Jeez, I almost need another bit of help in how to phrase this question, New Java II student here.天哪,我几乎需要另一个帮助来回答这个问题,这里是新的 Java II 学生。 thanks in advance for your time.
在此先感谢您的时间。
I have a list of employees that look like this:我有一个看起来像这样的员工列表:
public class Employee {
private String name;
private String department;
}
And a list of Companies that look like this:以及如下所示的公司列表:
public class Company {
private String name;
List<Department> departments;
}
Department is just:部门只是:
public class Department{
private String name;
private Integer totalSalary;
}
So I'm tasked with streaming a list of employees that work for the same company.因此,我的任务是流式传输为同一家公司工作的员工列表。 (Sorry for not saying before: the company is passed in to a fuction. It's the lone argument) It seemed easy when I first read it, but because of how the classes are set up, (Company with only a list of departments, and Employee with a single department, but no link between employee and company) I can stream to make a list of all Departments in a Company, but just don't know how to bring that back and match the employee's department string with any string from the departments that belong to the Company in question...
(抱歉之前没有说:公司被传递给一个函数。这是唯一的论点)我第一次阅读时似乎很容易,但由于课程的设置方式,(公司只有一个部门列表,并且员工只有一个部门,但员工和公司之间没有联系)我可以 ZF7B44CFFAFD5C52223D5498196C8A2E7BZ 列出公司中的所有部门,但只是不知道如何将其带回来并将员工的部门字符串与来自属于该公司的部门...
List<Department> deptsInCompany = companies.stream()
.filter(s -> s.getName().equals(passedInCompany))
.flatMap(s -> s.getDepartments().stream())
.collect(Collectors.toList());
I'm just not sure how to use that list of departments to backtrack and find the Employees in those departments.我只是不确定如何使用该部门列表来回溯并找到这些部门的员工。 I think my ROOKIE mind can't get past wanting there to be a list of Employees in each department object, but there's not!
我认为我的 ROOKIE 头脑无法摆脱想要每个部门 object 的员工列表,但没有!
Any little nudge would be greatly appreciated!任何小小的推动将不胜感激! I promise to pay it forward when i've got some skill!!
当我有一些技巧时,我会向 promise 付款!
Assuming that you have a list of all employees and that all your model classes have getters for their properties, you can do the following:假设您有一个所有员工的列表,并且您的所有 model 类的属性都有 getter,您可以执行以下操作:
public static void main(String[] args) {
List<Company> companies = // Your list of Companies
String passedInCompany = "Company";
List<String> deptsNameInCompany = companies.stream()
.filter(s -> s.getName().equals(passedInCompany))
.flatMap(s -> s.getDepartments().stream())
.map(Department::getName)
.collect(Collectors.toList());
List<Employee> employees = // All Employees
List<Employee> employeesInCompanyDepts = employees.stream()
.filter(employee -> deptsNameInCompany.contains(employee.getDepartment()))
.collect(Collectors.toList());
}
Basically you need to collect all the Department
s names and then find the Employee
s that have such Department
name in its department
property.基本上,您需要收集所有
Department
的名称,然后在其department
属性中找到具有此类Department
名称的Employee
。
Collect the department names of the (single) company with the given name into a Set
(which is faster for lookup than a List).将具有给定名称的(单个)公司的部门名称收集到一个
Set
中(查找比列表更快)。
Set<String> departmentNames = companies.stream()
.filter(c -> c.getName().equals(companyName))
.findFirst().get().getDepartments().stream()
.map(Department::getName)
.collect(Collectors.toSet());
Then remove all employees that aren't in those departments from the list.然后从列表中删除不在这些部门中的所有员工。
employees.removeIf(e -> !departmentNames.contains(e.getDepartment()));
If you want to preserve the list of employees, filter and collect:如果要保留员工列表,请过滤并收集:
List<Employee> employeesInCompany = employees.stream()
.filter(e -> departmentNames.contains(e.getDepartment()))
.collect(Collectors.toList());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.