Sets intersection() function | Guava | Java
Guava’s Sets.intersection() returns an unmodifiable view of the intersection of two sets. The returned set contains all elements that are contained by both backing sets. The iteration order of the returned set matches that of set1.
Syntax:
public static <E>
Sets.SetView<E>
intersection(Set<E> set1, Set<?> set2)
Return Value: This method returns an unmodifiable view of the intersection of two sets.
Below examples illustrate the working of Sets intersection method:
Example 1:
// Java code to show implementation // of Guava's Sets.intersection() 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(10, 20, 30, 40, 50); // Creating second set Set<Integer> set2 = Sets.newHashSet(30, 50, 70, 90); // Using Guava's Sets.intersection() method Set<Integer> answer = Sets.intersection(set1, set2); // Displaying the intersection of set1 and set2 System.out.println("Set 1: " + set1); System.out.println("Set 2: " + set2); System.out.println("Set 1 intersection Set 2: " + answer); } } |
Set 1: [40, 10, 50, 20, 30] Set 2: [50, 90, 30, 70] Set 1 intersection Set 2: [50, 30]
Example 2:
// Java code to show implementation // of Guava's Sets.intersection() 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.intersection() method Set<String> answer = Sets.intersection(set1, set2); // Displaying the intersection of set1 and set2 System.out.println("Set 1: " + set1); System.out.println("Set 2: " + set2); System.out.println("Set 1 intersection Set 2: " + answer); } } |
Set 1: [k, s, e, G] Set 2: [e, f, g, G] Set 1 intersection Set 2: [e, G]
Note: The returned view performs slightly better when set1 is the smaller of the two sets. If you have reason to believe one of your sets will generally be smaller than the other, pass it first.
Recommended Posts:
- Sets powerSet() function | Guava | Java
- Sets union() function | Guava | Java
- Sets difference() function | Guava | Java
- Ints min() function | Guava | Java
- Ints max() function | Guava | Java
- Ints contains() function | Guava | Java
- Ints asList() function | Guava | Java
- Ints lastIndexOf() function | Guava | Java
- Ints concat() function | Guava | Java
- Ints toArray() function | Guava | Java
- BigIntegerMath binomial() function | Guava | Java
- BigIntegerMath sqrt() function | Guava | Java
- BigIntegerMath log10() function | Guava | Java
- Ints indexOf() function | Guava | Java
- BigIntegerMath floorPowerOfTwo() 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.



