Without redefining operators, you have to convert the Iterable
to a Set
manually.
val Set<Integer> setA = #{1, 2, 3}
val Set<Integer> setB = (#{3, 4, 5} + setA).toSet
If you have a mutable set, there is one other way: The +=
operator, which is a shortcut for addAll
on any collection.
val Set<Integer> setA = #{1, 2, 3}
val Set<Integer> setB = newHashSet(3, 4, 5)
setB += setA
Neither solution look particularly nice and you probably want to avoid mutability.
As suggested in the other answer, Guava's Sets.union
method might come in handy, though I would rather import it as static extension than redefining operators. Then you can use:
val Set<Integer> setA = #{1, 2, 3}
val Set<Integer> setB = #{3, 4, 5}.union(setA)
Be careful though, union
returns a view of both sets, which can change if the underlying sets are mutable.