I do not think that there is a reduction operation in Guava. I think you have two options.
If you are using java-8 , just go through the recordset and collect the records into a new map using groupingBy and reduce .
import static java.util.stream.Collectors.groupingBy; import static java.util.stream.Collectors.mapping; import static java.util.stream.Collectors.reducing; ... Map<Integer, Integer> map = multimap.entries() .stream() .collect(groupingBy(Map.Entry::getKey, reducing(1, Map.Entry::getValue, (a, b) -> a * b)));
groupingBy , as you might guess, groups records by their key values. Then we simply summarize the values โโgrouped (which are Entry<Integer, Integer> ), first matching them with their values โโand finally by multiplying them, providing an identity value of 1 for the multiplication.
<h / "> If you cannot use java-8 , you can return Map<Integer, Collection<Integer>> from Multimap using Multimaps.asMap and use Maps.transformValues :
Map<Integer, Collection<Integer>> tempMap = Multimaps.asMap(oldMap); Map<Integer, Integer> newMap = Maps.transformValues(tempMap, new Function<Collection<Integer>, Integer>() { @Nullable @Override public Integer apply(Collection<Integer> input) {
source share