org.agzamovr.collectors.MultiValueMapCollector Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of collectorex Show documentation
Show all versions of collectorex Show documentation
Java Collectors extensions for using with Java 8 streams
The newest version!
package org.agzamovr.collectors;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.stream.Collector;
import java.util.stream.Collector.Characteristics;
class MultiValueMapCollector {
static final MultiValueMapCollector MULTI_VALUE_MAP_COLLECTOR = new MultiValueMapCollector();
Map multiValueMapFinisher(Map> map,
Collector super V, ?, R> downstream) {
Map resultMap = new HashMap<>();
map.forEach((key, value) -> resultMap.put(key, value.stream().collect(downstream)));
return resultMap;
}
void accumulator(Map> map, Map item) {
item.forEach((key, value) -> map.computeIfAbsent(key, k -> new ArrayList<>()).add(value));
}
Map> combiner(Map> left, Map> right) {
right.forEach((key, value) -> left.merge(key, value, CollectorEx::listCombiner));
return left;
}
Collector