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

se.ugli.java.util.stream.Collectors Maven / Gradle / Ivy

There is a newer version: 1.8.2.4
Show newest version
package se.ugli.java.util.stream;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collector;

import se.ugli.java.util.ImmutableList;
import se.ugli.java.util.ImmutableMap;
import se.ugli.java.util.ImmutableSet;
import se.ugli.java.util.juc.ImmutableListImpl;
import se.ugli.java.util.juc.ImmutableMapImpl;
import se.ugli.java.util.juc.ImmutableSetImpl;

public class Collectors {

    private Collectors() {
    }

    public static  Collector> toImmutableSet(final Supplier> mutableSetFactory,
            final Function, ImmutableSet> immutableSetFactory) {
        return Collector.of(mutableSetFactory, (list, e) -> list.add(e), (left, right) -> {
            left.addAll(right);
            return left;
        }, immutableSetFactory);
    }

    public static  Collector> toImmutableList(final Supplier> mutableListFactory,
            final Function, ImmutableList> immutableListFactory) {
        return Collector.of(mutableListFactory, (list, e) -> list.add(e), (left, right) -> {
            left.addAll(right);
            return left;
        }, immutableListFactory);
    }

    public static  Collector> toImmutableList() {
        return toImmutableList(ArrayList::new, list -> new ImmutableListImpl<>(list));
    }

    public static  Collector> toImmutableSet() {
        return toImmutableSet(HashSet::new, set -> new ImmutableSetImpl<>(set));
    }

    public static  Collector> toImmutableMap(
            final Function keyMapper, final Function valueMapper) {
        return toImmutableMap(keyMapper, valueMapper, HashMap::new, map -> new ImmutableMapImpl<>(map));
    }

    public static  Collector> toImmutableMap(
            final Function keyMapper, final Function valueMapper,
            final Supplier> mutableMapFactory,
            final Function, ImmutableMap> immutableMapFactory) {
        return Collector.of(mutableMapFactory,
                (map, element) -> map.put(keyMapper.apply(element), valueMapper.apply(element)), (m1, m2) -> {
                    m1.putAll(m2);
                    return m1;
                }, immutableMapFactory);
    }

    public static  Collector>> groupingBy(
            final Function classifier) {
        return Collector.of((Supplier>>) HashMap::new, (m, t) -> {
            m.computeIfAbsent(classifier.apply(t), k -> new ArrayList<>()).add(t);
        }, (m1, m2) -> {
            m2.entrySet().forEach(e -> {
                m1.computeIfAbsent(e.getKey(), k -> new ArrayList<>()).addAll(e.getValue());
            });
            return m1;
        }, map -> {
            final Map> newMap = new HashMap<>();
            map.entrySet().forEach(e -> newMap.put(e.getKey(), new ImmutableListImpl<>(e.getValue())));
            return new ImmutableMapImpl<>(newMap);
        });
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy