将列表中的列表提取为单个列表

extract list inside a list as a single list

我有两个 POJO, 下面的示例代码

class A {
    String name;
    Object entries; // this can be a List<String> or a string - hence Object datatype
   //getters and setter here
}

class B {
    int snumber;
    List<A> values;
   //getters and setters here
}

控制器class

class ControllerA {
    public getList(B b) {
        List<String> list = b.getValues().stream.map(e -> e.getEntries()).collect(Collectors.toList()));
    }
}

这个returns我一个榜单:

[[12345, 09876], [567890, 43215]]

但我想要的是像

这样的单个列表
[12345,09876,567890, 43215]

如何使用 Java 8 个流来做到这一点?

我也试过 flatMap,但这与条目的 Object 数据类型不匹配。

List<String> 视为 A class 中的字段 entries

如评论中@Eugene所述,

If it's a single String make it a List of a single element; if it's a list of multiple strings make it as such.

使用单一类型的集合可以简化过程:

b.getValues()                           // -> List<A>
        .stream()                       // -> Stream<A>
        .map(A::getEntries)             // -> Stream<List<String>>
        .flatMap(Collection::stream)    // -> Stream<String>
        .collect(Collectors.toList());  // -> List<String>

你也可以用。

b.getValues()                   // -> List<A>
    .stream()                   // -> Stream<A>
    .map(A::getEntries)         // -> Stream<List<String>>
    .flatMap(Collection::stream)// -> Stream<String>
    .toList();