Sets difference() function | Guava | Java
Guava’s Sets.difference() returns an unmodifiable view of the difference of two sets.
public static <E> Sets.SetView<E> difference(Set<E> set1, Set<?> set2)
Return Value: This method returns a set containing all elements that are contained by set1 and not contained by set2.
Note: Set2 may also contain elements not present in set1, these are simply ignored. The iteration order of the returned set matches that of set1.
Set 1: [1, 2, 3, 4, 5, 6] Set 2: [1, 3, 5, 7] Difference between Set 1 and Set 2: [2, 4, 6]
Example 2 :
Set 1: [E, G, H, L, O] Set 2: [I, K, L, E, G] Difference between Set 1 and Set 2: [H, O]
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. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.