Why not using:
coll.stream().collect(Collectors.toImmutableSet())
As Collectors.toImmutableSet() is currently implemented, with serial Stream it
will create a single HashSet, add all the elements to it and call
Set.of(HashSet.toArray()) with it. Pretty much the same as what Tagir
proposes, but the Collector could be made more efficient in the future and
with it, the optimization would automatically extend to Set.copyOf()...
This is mainly about whether Set.copyOf() is implemented in terms of
Collectors.toUnmodifiableSet(), or vice-versa, which then calls Set.of(T[]) to
do the actual creation. Some future optimization will probably replace both of
these implementations with calls to JDK internal methods that can bypass the
extra copying, so it doesn't really matter which one of these calls the other
right now.
s'marks