usethesource / capsule

The Capsule Hash Trie Collections Library
BSD 2-Clause "Simplified" License
404 stars 27 forks source link

Reintroduce CapsuleCollectors.toMap? #34

Open victornoel opened 2 years ago

victornoel commented 2 years ago

Hi,

I see that CapsuleCollectors.toMap is commented in the code. Is there any reason not to make it available?

That's how I reimplemented it:

    public static <T, K, V> Collector<T, ?, io.usethesource.capsule.Map.Immutable<K, V>> toMap(
        Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends V> valueMapper
    ) {
        /** extract key/value from type {@code T} and insert into map */
        final BiConsumer<io.usethesource.capsule.Map.Transient<K, V>, T> accumulator =
            (map, element) -> map.__put(keyMapper.apply(element), valueMapper.apply(element));

        return new DefaultCollector<>(
            io.usethesource.capsule.Map.Transient::of,
            accumulator,
            (left, right) -> {
                left.__putAll(right);
                return left;
            },
            io.usethesource.capsule.Map.Transient::freeze,
            CapsuleCollectors.UNORDERED
        );
    }

Another one would be the same but using a function to merge values in case one already exists in the transient map.

Thanks!