如何将具有列表属性的对象列表转换为包含所有列表属性迭代的列表

Fre*_*000 3 java guava

我有一个像这样的对象:

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>>结果.

Eti*_*veu 9

如果您确实需要使用函数方法,可以使用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)


Lou*_*man 6

FluentIterable.from(listOfHouses).transformAndConcat(doorFunction)
Run Code Online (Sandbox Code Playgroud)

会做得很好.