我希望能够使用lambdaj将子列表合并为一个列表。

我有一个迭代的版本,可以工作:

// HDU elements are children of each subsystem
Collection<SpaceSystemType> subsystems = this.getAllSubsystems();
Set<SpaceSystemType> sources = new HashSet<SpaceSystemType>();

// Iterate the subsystems, collecting all the sources
for (SpaceSystemType subsystem : subsystems)
    sources.addAll(subsystem.getSpaceSystem()); // getSpaceSystem returns a List<SpaceSystemType>

return sources;


我希望能够做到这一点:

extract(subsystems, on(SpaceSystemType.class).getSpaceSystem());


但是提取返回一个

List<List<SpaceSystemType>>


所以我一定使用了错误的命令。

哪个lambdaj命令可以实现我想要的功能?

最佳答案

我用flatten解决了这个问题:

List<SpaceSystemType> sources = flatten(extract(subsystems, on(SpaceSystemType.class).getSpaceSystem()));


SpaceSystemType是JAXB生成的类,表示元素的子树。由于SpaceSystemType.getSpaceSystem()返回一个List,因此有必要指示lambdaj从树中获取所有叶子。

10-06 12:52