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

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

The newest version!
package org.agzamovr.collectors;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collector;

class DistinctCollector {
    static final DistinctCollector DISTINCT_COLLECTOR = new DistinctCollector();

    
    R distinctFinisher(Map map,
                       Collector downstream) {
        return map.values().stream().collect(downstream);
    }

    
    void accumulator(Map map, T item, Function mapper) {
        map.put(mapper.apply(item), item);
    }

    
    Map combiner(Map left, Map right) {
        left.putAll(right);
        return left;
    }

    
    Collector, Collection> distinct(Function mapper) {
        return Collector.of(HashMap::new,
                (map, item) -> accumulator(map, item, mapper),
                this::combiner,
                Map::values);
    }

    
    Collector, R> distinct(Function mapper,
                                        Collector downstream) {
        return Collector.of(HashMap::new,
                (map, item) -> accumulator(map, item, mapper),
                this::combiner,
                (map) -> distinctFinisher(map, downstream));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy