How would you make a java.util.stream.Collector that collects to com.google.common.collect.ImmutableSet?

谁都会走 提交于 2019-12-08 19:51:42

问题


That seems too tricky for me since ImmutableSet instances are only built with ImmutableSet.Builder instances, which don't implement Collection so you can't just use Collectors.toCollection(ImmutableSet::new) or Collectors.toCollection(ImmutableSet.Builder::new).


回答1:


This is built into guava now,

ImmutableSet#toImmutableSet

Use like,

something.stream().collect(ImmutableSet.toImmutableSet())



回答2:


In fact, 3 months after :-), instead of defining a whole class for this, you can use Collector.ofand wrap it in a simple utility method:

public static <T> Collector<T, Builder<T>, ImmutableSet<T>> immutableSetCollector() {
    return Collector.of(Builder<T>::new, Builder<T>::add, (s, r) -> s.addAll(r.build()), Builder<T>::build);
}

and then:

import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSet.Builder;
....

ImmutableSet<Point2D> set = 
    Stream.of(new Point2D(1, 2), ...).collect(immutableSetCollector());



回答3:


We can write custom collector by extending the Collector interface.

Here is a solid reference that talks about it good detail: http://java.dzone.com/articles/introduction-writing-custom




回答4:


This is the closest that I found:

Set<String> set = list.stream().collect(ImmutableSet.Builder<String>::new, ImmutableSet.Builder<String>::add, (builder1, builder2) -> builder1.addAll(builder2.build())).build();

There is no method that takes append the elements of one builder into another builder, only an Iterable or an Iterator, so you cannot use a method reference for this one.




回答5:


So it appears that writing a custom collector in this case is not such a difficult task as I imagined it to be:

package org.tendiwa.collections;

import com.google.common.collect.ImmutableSet;

import java.util.Set;
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;

public class ImmutableSetCollector<T> implements Collector<T, ImmutableSet.Builder<T>, ImmutableSet<T>> {

    @Override
    public Supplier<ImmutableSet.Builder<T>> supplier() {
        return ImmutableSet.Builder::new;
    }

    @Override
    public BiConsumer<ImmutableSet.Builder<T>, T> accumulator() {
        return (builder, element) -> builder.add(element);
    }

    @Override
    public BinaryOperator<ImmutableSet.Builder<T>> combiner() {
        return (b1, b2) -> b1.addAll(b2.build());
    }

    @Override
    public Function<ImmutableSet.Builder<T>, ImmutableSet<T>> finisher() {
        return ImmutableSet.Builder::build;
    }

    @Override
    public Set<Characteristics> characteristics() {
        return ImmutableSet.of();
    }
}

Usage:

public static void main(String[] args) {
    ImmutableSet<Point2D> set = Arrays.asList(
        new Point2D(1, 2),
        new Point2D(2, 4),
        new Point2D(3, 5),
        new Point2D(4, 4),
        new Point2D(5, 6),
        new Point2D(8, 6)
    ).stream().collect(new ImmutableSetCollector<>());
    System.out.println(set);
}

Output:

[{1.0:2.0}, {2.0:4.0}, {3.0:5.0}, {4.0:4.0}, {5.0:6.0}, {8.0:6.0}]



回答6:


This functionality is available out-of-the-box in the guava-jdk8 library (which I authored).



来源:https://stackoverflow.com/questions/27612165/how-would-you-make-a-java-util-stream-collector-that-collects-to-com-google-comm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!