我可以帮助你翻译,以下是需要翻译的内容:
我正在尝试使用Java流将下面的嵌套for循环转换为哈希映射,但我在收集器步骤中遇到了困难。你能帮忙吗?
现有代码:
private static HashMap<String, Long> getOutput(List<Employee> eList) {
HashMap<String, Long> outputList = new HashMap<>();
for (Employee employee : eList) {
List<Department> departmentList = employee.getDepartmentList();
for (Department department : departmentList) {
if (department.getType().equals(DepartmentType.SCIENCE)) {
outputList.put(employee.getName(),department.getDepartmentId()));
}
}
}
return outputList;
}
到目前为止我尝试过:
private static HashMap<String, Long> getOutput(List<Employee> eList) {
return eList.stream()
.flatMap(emp -> emp.getDepartmentList().stream()
.filter(dept -> dept.getType().equals(DepartmentType.SCIENCE))
.collect(HashMap::new, ???)
}
outputList
和OutputList
的拼写不一致,而且它的类型是Map
,与方法的返回类型HashMap
不匹配。如果这是“现有代码”,为什么不直接从IDE中复制可工作的代码,而要在浏览器中进行原型设计呢? - Holger