如何使用 lambdaj 组合列表列表?
我希望能够使用 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());
但是 extract 返回 a
List<List<SpaceSystemType>>
所以我必须使用错误的命令。
哪个 lambdaj 命令可以实现我想要的?
I want to be able to combine sublists into a single list using lambdaj.
I have an iterative version that works:
// 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;
I'd like to be able to do this:
extract(subsystems, on(SpaceSystemType.class).getSpaceSystem());
But extract returns a
List<List<SpaceSystemType>>
so I must be using the wrong command.
Which lambdaj command achieves what I want?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我使用展平解决了这个问题:
SpaceSystemType 是一个 JAXB 生成的类,表示元素的子树。由于 SpaceSystemType.getSpaceSystem() 返回一个 List,因此有必要指示 lambdaj 从树中获取所有叶子。
I solved this using flatten:
SpaceSystemType is a JAXB-generated class representing a subtree of elements. Since SpaceSystemType.getSpaceSystem() returns a List, it's necessary to direct lambdaj to take all of the leaves from the tree.
我发现@retrodone 的答案不太容易理解。这是另一个例子:
I found @retrodone 's answer not so easy to understand. Here's another example: