How can I transform a collection into a Guava Multimap grouped by the elements of a nested collection property?(如何将集合转换为按嵌套集合属性的元素分组的 Guava Multimap?)
问题描述
我有一个 List<Foo>
并且想要一个 Guava Multimap<String, Foo>
我们将 Foo
的每个标签分组集合<字符串>getTags()
函数.
I have a List<Foo>
and want a Guava Multimap<String, Foo>
where we've grouped the Foo
s by each tag of their Collection<String> getTags()
function.
我使用的是 Java 8,因此 lambda 和方法引用很好/值得鼓励.
I am using Java 8, so lambdas and method references are fine/encouraged.
例如,如果我有:
foo1, tags=a,b,c
foo2, tags=c,d
foo3, tags=a,c,e
我会得到一个 Multimap<String, Foo>
:
a -> foo1, foo3
b -> foo1
c -> foo1, foo2, foo3
d -> foo2
e -> foo3
推荐答案
您可以为此使用自定义收集器:
You can use custom collector for this:
Multimap<String, Foo> map = list.stream().collect(
ImmutableMultimap::builder,
(builder, value) -> value.getTags().forEach(tag -> builder.put(tag, value)),
(builder1, builder2) -> builder1.putAll(builder2.build())
).build();
这不会导致额外的副作用(请参阅 here on this),是并发且更惯用的.
This does not cause extra side effects (see here on this), is concurrent and more idiomatic.
您还可以将这些临时 lambda 提取到成熟的收集器中,如下所示:
You can also extract these ad-hoc lambdas into a full-fledged collector, something like this:
public static <T, K> Collector<T, ?, Multimap<K, T>> toMultimapByKey(Function<? super T, ? extends Iterable<? extends K>> keysMapper) {
return new MultimapCollector<>(keysMapper);
}
private static class MultimapCollector<T, K> implements Collector<T, ImmutableMultimap.Builder<K, T>, Multimap<K, T>> {
private final Function<? super T, ? extends Iterable<? extends K>> keysMapper;
private MultimapCollector(Function<? super T, ? extends Iterable<? extends K>> keysMapper) {
this.keysMapper = keysMapper;
}
@Override
public Supplier<ImmutableMultimap.Builder<K, T>> supplier() {
return ImmutableMultimap::builder;
}
@Override
public BiConsumer<ImmutableMultimap.Builder<K, T>, T> accumulator() {
return (builder, value) -> keysMapper.apply(value).forEach(k -> builder.put(k, value));
}
@Override
public BinaryOperator<ImmutableMultimap.Builder<K, T>> combiner() {
return (b1, b2) -> b1.putAll(b2.build());
}
@Override
public Function<ImmutableMultimap.Builder<K, T>, Multimap<K, T>> finisher() {
return ImmutableMultimap.Builder<K, T>::build;
}
@Override
public Set<Characteristics> characteristics() {
return Collections.emptySet();
}
}
那么集合将如下所示:
Multimap<String, Foo> map = list.stream().collect(toMultimapByKey(Foo::getTags));
如果顺序对您不重要,您也可以从 characteristics()
方法返回 EnumSet.of(Characteristics.UNORDERED)
.这可以使内部收集机制更有效地发挥作用,尤其是在并行归约的情况下.
You can also return EnumSet.of(Characteristics.UNORDERED)
from characteristics()
method if the order is not important for you. This can make internal collection machinery act more efficiently, especially in case of parallel reduction.
这篇关于如何将集合转换为按嵌套集合属性的元素分组的 Guava Multimap?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何将集合转换为按嵌套集合属性的元素分组的 Guava Multimap?
基础教程推荐
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01