All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.agzamovr.collectors.MultiValueMapCollector Maven / Gradle / Ivy

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 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, ?, Map>> mapStreamToMultiValueMap() {
        return Collector.of(HashMap::new,
                this::accumulator,
                this::combiner,
                Characteristics.IDENTITY_FINISH);
    }

    
    Collector, Map>, Map> mapStreamToMultiValueMap(Collector downstream) {
        return Collector.of(HashMap::new,
                this::accumulator,
                this::combiner,
                (map) -> multiValueMapFinisher(map, downstream));
    }

    
    Collector, ?, Map>> entryStreamToMultiValueMap() {
        return toMultiValueMap(Entry::getKey, Entry::getValue);
    }

    
    Collector, Map>, Map> entryStreamToMultiValueMap(Collector downstream) {
        return toMultiValueMap(Entry::getKey, Entry::getValue, downstream);
    }

    
    Collector>, Map>> toMultiValueMap(Function keyMapper,
                                                                   Function valueMapper) {
        BiConsumer>, T> accumulator
                = (map, element) -> {
            K key = keyMapper.apply(element);
            V value = valueMapper.apply(element);
            List list = map.computeIfAbsent(key, k -> new ArrayList<>());
            list.add(value);
        };
        return Collector.of(HashMap::new,
                accumulator,
                this::combiner,
                Characteristics.IDENTITY_FINISH);
    }

    
    Collector>, Map> toMultiValueMap(Function keyMapper,
                                                             Function valueMapper,
                                                             Collector downstream) {
        BiConsumer>, T> accumulator
                = (map, element) -> {
            K key = keyMapper.apply(element);
            V value = valueMapper.apply(element);
            List list = map.computeIfAbsent(key, k -> new ArrayList<>());
            list.add(value);
        };
        return Collector.of(HashMap::new,
                accumulator,
                this::combiner,
                (map) -> multiValueMapFinisher(map, downstream));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy