在Java8地图中使用函数

约翰·鲍姆

是否有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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章