更新:我发现了一个似乎涵盖了所有Guava集合的实现在https://github.com/yanaga/guava-stream,并尝试在我的库中进行改进在https://bitbucket.org/cowwoc/guava-jdk8/
出于历史原因,我将保留下面的先前回答。
天啊 #@!( 我搞定它了!
这个实现适用于任何Multimap(可变或不可变),而shmosel的解决方案则专注于不可变实现。也就是说,后者可能对于不可变情况更有效率(我没有使用构建器)。
import com.google.common.collect.Multimap;
import java.util.EnumSet;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collector;
import java.util.stream.Collector.Characteristics;
import org.bitbucket.cowwoc.preconditions.Preconditions;
public final class MultimapCollector<T, K, V, R extends Multimap<K, V>>
implements Collector<T, Multimap<K, V>, R>
{
private final Supplier<Multimap<K, V>> mapSupplier;
private final Function<? super T, ? extends K> keyMapper;
private final Function<? super T, ? extends V> valueMapper;
private final Function<Multimap<K, V>, R> resultMapper;
public MultimapCollector(Supplier<Multimap<K, V>> mapSupplier,
Function<? super T, ? extends K> keyMapper,
Function<? super T, ? extends V> valueMapper,
Function<Multimap<K, V>, R> resultMapper)
{
Preconditions.requireThat(mapSupplier, "mapSupplier").isNotNull();
Preconditions.requireThat(keyMapper, "keyMapper").isNotNull();
Preconditions.requireThat(valueMapper, "valueMapper").isNotNull();
Preconditions.requireThat(resultMapper, "resultMapper").isNotNull();
this.mapSupplier = mapSupplier;
this.keyMapper = keyMapper;
this.valueMapper = valueMapper;
this.resultMapper = resultMapper;
}
@Override
public Supplier<Multimap<K, V>> supplier()
{
return mapSupplier;
}
@Override
public BiConsumer<Multimap<K, V>, T> accumulator()
{
return (map, entry) ->
{
K key = keyMapper.apply(entry);
if (key == null)
throw new IllegalArgumentException("keyMapper(" + entry + ") returned null");
V value = valueMapper.apply(entry);
if (value == null)
throw new IllegalArgumentException("keyMapper(" + entry + ") returned null");
map.put(key, value);
};
}
@Override
public BinaryOperator<Multimap<K, V>> combiner()
{
return (left, right) ->
{
left.putAll(right);
return left;
};
}
@Override
public Function<Multimap<K, V>, R> finisher()
{
return resultMapper;
}
@Override
public Set<Characteristics> characteristics()
{
return EnumSet.noneOf(Characteristics.class);
}
}
[...]
import com.google.common.collect.HashMultimap;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.Multimap;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collector;
public final class GuavaCollectors
{
public static <T, K, V, R extends Multimap<K, V>> Collector<T, ?, R> toMultimap(
Supplier<Multimap<K, V>> mapSupplier,
Function<? super T, ? extends K> keyMapper,
Function<? super T, ? extends V> valueMapper,
Function<Multimap<K, V>, R> resultMapper)
{
return new MultimapCollector<>(mapSupplier, keyMapper, valueMapper, resultMapper);
}
public static void main(String[] args)
{
Multimap<Integer, Double> input = HashMultimap.create();
input.put(10, 20.0);
input.put(10, 25.0);
input.put(50, 60.0);
System.out.println("input: " + input);
ImmutableMultimap<Integer, Double> output = input.entries().stream().collect(
GuavaCollectors.toMultimap(HashMultimap::create,
entry -> entry.getKey() + 1, entry -> entry.getValue() - 1,
ImmutableMultimap::copyOf));
System.out.println("output: " + output);
}
}
main() 的输出结果:
input: {10=[20.0, 25.0], 50=[60.0]}
output: {51=[59.0], 11=[24.0, 19.0]}
资源