Guava has collectors like ImmutableSet.toImmutableSet()
and ImmutableList.toImmutableList()
. Are they thread-safe? Is it safe to use them with parallel streams?
As far as I see the use the regular builders which are not thread-safe:
private static final Collector<Object, ?, ImmutableSet<Object>> TO_IMMUTABLE_SET =
Collector.of(
ImmutableSet::<Object>builder,
ImmutableSet.Builder::add,
ImmutableSet.Builder::combine,
ImmutableSet.Builder::build);
On the other hand, the first and third parameters of Collector.of()
makes it suspicious that JDK could create a separate builder for every parallel thread and combine their result to make it thread-safe.