Guava’s Sets.union() returns an unmodifiable view of the union of two sets. The returned set contains all elements that are contained in either backing set. Iterating over the returned set iterates first over all the elements of set1, then over each element of set2, in order, that is not contained in set1.
public static <E> Sets.SetView<E> union(Set<? extends E> set1, Set<? extends E> set2)
Return Value: This method returns an unmodifiable view of the union of two sets.
Set 1: [1, 2, 3, 4, 5] Set 2: [9, 3, 5, 7] Set 1 union Set 2: [1, 2, 3, 4, 5, 9, 7]
Set 1: [k, s, e, G] Set 2: [e, f, g, G] Set 1 union Set 2: [k, s, e, G, f, g]
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.