我有一个像这样的对象:
class House {
String name;
List<Door> doors;
}
Run Code Online (Sandbox Code Playgroud)
我想要做的是将a List<House>转换为List<Door>包含所有doors这一切的a houses.
有没有机会用番石榴做这个?
我尝试使用guava使用Lists.transform函数,但我只得到一个List<List<Door>>结果.
如果您确实需要使用函数方法,可以使用FluentIterable#transformAndConcat执行此操作:
public static ImmutableList<Door> foo(List<House> houses) {
return FluentIterable
.from(houses)
.transformAndConcat(GetDoorsFunction.INSTANCE)
.toImmutableList();
}
private enum GetDoorsFunction implements Function<House, List<Door>> {
INSTANCE;
@Override
public List<Door> apply(House input) {
return input.getDoors();
}
}
Run Code Online (Sandbox Code Playgroud)
FluentIterable.from(listOfHouses).transformAndConcat(doorFunction)
Run Code Online (Sandbox Code Playgroud)
会做得很好.
| 归档时间: |
|
| 查看次数: |
1670 次 |
| 最近记录: |