是否有Java8等效的方式编写以下内容?
List<Foo> results = new ArrayList<>();
for (RowResult row : rows) {
results.add(rowToFooFunction(classroomLookup.get(row.getId()), studentLookup.get(row.getId())).apply(row));
}
rowToFooFunction就像 Function<RowResult, Foo> rowToFoo (Classroom c, Student s)...
因此,我想得出以下结果:
rows.stream.map(rowToFooFunction...).collect(Collectors.toList());
问题是,在映射步骤中,我需要通过我要遍历的行的ID查找学生/教室。
您几乎已经自己找到了解决方案,您非常接近:
List<Foo> results = rows.stream().map(row ->
rowToFooFunction(classroomLookup.get(row.getId()), studentLookup.get(row.getId())).apply(row))
).collect(Collectors.toList());
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句