Sets union() function | Guava | Java
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.
Syntax:
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.
Example 1:
// Java code to show implementation // of Guava's Sets.union() method import com.google.common.collect.Sets; import java.util.Set; class GFG { // Driver's code public static void main(String[] args) { // Creating first set Set<Integer> set1 = Sets.newHashSet(1, 2, 3, 4, 5); // Creating second set Set<Integer> set2 = Sets.newHashSet(3, 5, 7, 9); // Using Guava's Sets.union() method Set<Integer> answer = Sets.union(set1, set2); // Displaying the union of set set1 and set2 System.out.println("Set 1: " + set1); System.out.println("Set 2: " + set2); System.out.println("Set 1 union Set 2: " + answer); } } |
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]
Example 2:
// Java code to show implementation // of Guava's Sets.union() method import com.google.common.collect.Sets; import java.util.Set; class GFG { // Driver's code public static void main(String[] args) { // Creating first set Set<String> set1 = Sets.newHashSet("G", "e", "e", "k", "s"); // Creating second set Set<String> set2 = Sets.newHashSet("g", "f", "G", "e"); // Using Guava's Sets.union() method Set<String> answer = Sets.union(set1, set2); // Displaying the union of set set1 and set2 System.out.println("Set 1: " + set1); System.out.println("Set 2: " + set2); System.out.println("Set 1 union Set 2: " + answer); } } |
Set 1: [k, s, e, G] Set 2: [e, f, g, G] Set 1 union Set 2: [k, s, e, G, f, g]
Recommended Posts:
- Sets difference() function | Guava | Java
- Sets intersection() function | Guava | Java
- Sets powerSet() function | Guava | Java
- Ints min() function | Guava | Java
- Ints max() function | Guava | Java
- Ints contains() function | Guava | Java
- BigIntegerMath log10() function | Guava | Java
- BigIntegerMath sqrt() function | Guava | Java
- BigIntegerMath ceilingPowerOfTwo() function | Guava | Java
- Ints join() function | Guava | Java
- Ints toArray() function | Guava | Java
- BigIntegerMath divide() function | Guava | Java
- BigIntegerMath floorPowerOfTwo() function | Guava | Java
- BigIntegerMath log2() function | Guava | Java
- BigIntegerMath isPowerOfTwo() function | Guava | Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



