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)
.
6 Answers
This is built into guava now,
Use like,
something.stream().collect(ImmutableSet.toImmutableSet())
In fact, 3 months after :-), instead of defining a whole class for this, you can use Collector.of
and 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());
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
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.
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}]
-
The link I pasted has code reference that does more or less what you have. Dec 23, 2014 at 0:06
-
@kuriouscoder Except that is says there in
charactersitics()
that the resulting collection is unordered, whenImmutableSet
actually has the user-specified order as per its documentation: docs.guava-libraries.googlecode.com/git/javadoc/com/google/…– gvlasovDec 23, 2014 at 0:09 -
Interesting. Isn't set unordered by default? docs.oracle.com/javase/8/docs/api/java/util/stream/… Dec 23, 2014 at 0:14
-
@kuriouscoder There is no constraint for Set ordering, so it is up to the implementation to decide. Characteristics.Unodrered just means that we can't rely on the set being ordered.– gvlasovDec 23, 2014 at 0:46
This functionality is available out-of-the-box in the guava-jdk8 library (which I authored).
-
Please honor the full disclosure guidelines. Noting bitbucket.org/cowwoc– DrewDec 29, 2015 at 15:24
-
@Drew I didn't know this was an issue but sure I'll update the answer accordingly.– GiliDec 30, 2015 at 3:25
-
Well as some of us review about a 1000 a day, this Reference ... you have a high rep. Problem is with rep <100 spamming. Thanks. Keep on truckin'– DrewDec 30, 2015 at 3:43
collectingAndThen
withtoCollection
?