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

jp.skypencil.guava.stream.MultimapCollector Maven / Gradle / Ivy

The newest version!
package jp.skypencil.guava.stream;

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 com.google.common.collect.LinkedListMultimap;
import com.google.common.collect.Multimap;

abstract class MultimapCollector>
        implements Collector, R> {
    private Function keyMapper;
    private Function valueMapper;

    MultimapCollector(Function keyMapper,
            Function valueMapper) {
        this.keyMapper = keyMapper;
        this.valueMapper = valueMapper;
    }

    @Override
    public Supplier> supplier() {
        return LinkedListMultimap::create;
    }

    @Override
    public BiConsumer, T> accumulator() {
        return (map, data) ->
            map.put(keyMapper.apply(data), valueMapper.apply(data))
        ;
    }

    @Override
    public BinaryOperator> combiner() {
        return (map, another) -> {
            map.putAll(another);
            return map;
        };
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy