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

org.kiwiproject.stream.KiwiMultimapCollectors Maven / Gradle / Ivy

Go to download

Kiwi is a utility library. We really like Google's Guava, and also use Apache Commons. But if they don't have something we need, and we think it is useful, this is where we put it.

There is a newer version: 4.5.2
Show newest version
package org.kiwiproject.stream;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.LinkedHashMultimap;
import com.google.common.collect.Multimap;
import lombok.experimental.UtilityClass;

import java.util.Map;
import java.util.function.Supplier;
import java.util.stream.Collector;

/**
 * A {@link Collector} that can collect into a Guava {@link Multimap}.
 */
@UtilityClass
public class KiwiMultimapCollectors {

    /**
     * Given a stream of {@link Map.Entry} objects, collects to the {@link Multimap} specified in the
     * {@code supplier} argument. This lets you create any type of Multimap you want to collect into. Usually you
     * will use a method reference as the supplier, e.g. {@code ArrayListMultimap:create}. You can easily get a
     * stream of Map.Entry objects by getting the map's {@link Map#entrySet()} and then calling stream on it, e.g.
     * {@code someMap.entrySet().stream()}.
     *
     * @param supplier the Multimap to collect into
     * @param       the key type of the Map.Entry objects
     * @param       the value type of the Map.Entry objects
     * @param       the accumulator type, i.e. some kind of Multimap
     * @return the collected Multimap
     */
    public static > Collector, A, A> toMultimap(Supplier supplier) {
        return Collector.of(
                supplier,
                (accumulator, entry) -> accumulator.put(entry.getKey(), entry.getValue()),
                (map1, map2) -> {
                    map1.putAll(map2);
                    return map1;
                });
    }

    /**
     * Collects into an {@link ArrayListMultimap}.
     *
     * @param  the key type of the Map.Entry objects
     * @param  the value type of the Map.Entry objects
     * @return the collected ArrayListMultimap
     */
    public static  Collector, Multimap, Multimap> toArrayListMultimap() {
        return toMultimap(ArrayListMultimap::create);
    }

    /**
     * Collects into an {@link HashMultimap}.
     *
     * @param  the key type of the Map.Entry objects
     * @param  the value type of the Map.Entry objects
     * @return the collected HashMultimap
     */
    public static  Collector, Multimap, Multimap> toHashMultimap() {
        return toMultimap(HashMultimap::create);
    }

    /**
     * Collects into an {@link LinkedHashMultimap}.
     *
     * @param  the key type of the Map.Entry objects
     * @param  the value type of the Map.Entry objects
     * @return the collected LinkedHashMultimap
     */
    public static  Collector, Multimap, Multimap> toLinkedHashMultimap() {
        return toMultimap(LinkedHashMultimap::create);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy