Java – the easiest way to index a collection’s attributes, which are themselves a collection
I have a list < foo > and want a guava Multimap < string, foo > we divide each tag of their collection < string > into foo gettags() function
I use Java 8, so Lambdas and method references are fine / encouraged
For example, if I have:
foo1,tags=a,b,c foo2,tags=c,d foo3,c,e
I will get a Multimap < string, foo >, which has:
a -> foo1,foo3 b -> foo1 c -> foo1,foo2,foo3 d -> foo2 e -> foo3
Solution
You can use custom favorites:
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();
This does not cause additional side effects (see here). It is concurrent and more common
You can also extract these special lambs into a complete collector, as shown below:
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>,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> 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>> finisher() {
return ImmutableMultimap.Builder<K,T>::build;
}
@Override
public Set<characteristics> characteristics() {
return Collections.emptySet();
}
}
Then the collection will be as follows:
Multimap<String,Foo> map = list.stream().collect(toMultimapByKey(Foo::getTags));
If your order is not important, you can also return enumset. From the attribute () method of(characteristics.UNORDERED). This can make the internal collection machine work more effectively, especially in the case of parallel restore
