为了账号安全,请及时绑定邮箱和手机立即绑定

使用 Guava 连接 ImmutableSet

使用 Guava 连接 ImmutableSet

GCT1015 2023-03-17 10:36:31
我已经习惯了 C#,IEnumerable<T>.SelectMany但发现自己使用 Google 的 Guava 库涉足了一些 Java 代码。番石榴中是否有等同于 SelectMany 的东西?示例:如果我有这样的流/映射结构collections             .stream()             .map(collection -> loadKeys(collection.getTenant(), collection.getGroup()))             .collect(GuavaCollectors.immutableSet());whereloadKeys返回类似的东西ImmutableSet<String>,这个函数会返回ImmutableSet<ImmutableSet<String>>,但我想把它们压平成一个ImmutableSet<String>最好的方法是什么?
查看完整描述

1 回答

?
跃然一笑

TA贡献1826条经验 获得超6个赞

您可以使用Stream::flatMap方法:

collections
        .stream()
        .flatMap(collection -> loadKeys(collection.getTenant(), collection.getGroup()).stream())
        .collect(ImmutableSet.toImmutableSet());

请注意,您得到了方法stream外的loadKeys结果。结果应该ImmutableSet<String>假设loadKeys返回一个集合。


查看完整回答
反对 回复 2023-03-17
  • 1 回答
  • 0 关注
  • 81 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信