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.
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:
Set 1: [40, 10, 50, 20, 30] Set 2: [50, 90, 30, 70] Set 1 intersection Set 2: [50, 30]
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.