【发布时间】:2017-09-26 16:27:00
【问题描述】:
我有这样的课
public class Example {
private List<Integer> ids;
public getIds() {
return this.ids;
}
}
如果我有这样的此类的对象列表
List<Example> examples;
如何将所有示例的 id 列表映射到一个列表中? 我试过这样:
List<Integer> concat = examples.stream().map(Example::getIds).collect(Collectors.toList());
但Collectors.toList() 出现错误
使用 Java 8 流 api 实现这一目标的正确方法是什么?
【问题讨论】:
-
What's the difference between map and flatMap methods in Java 8? 的可能重复项。即使它不是完美的复制品,那里的答案也很有用。
-
你的方法
getIds没有返回类型。