How to Find Intersection of Two Sets in Java Easily
To find the intersection of two sets in Java, use the
retainAll() method on one set, passing the other set as an argument. This method modifies the first set to keep only elements present in both sets, effectively giving you their intersection.Syntax
The retainAll() method is called on a Set object and takes another Collection as a parameter. It removes all elements from the first set that are not contained in the second set.
set1.retainAll(set2);- modifiesset1to keep only elements also inset2.
java
set1.retainAll(set2);
Example
This example shows how to find the intersection of two sets of strings. It prints the common elements between the two sets.
java
import java.util.Set; import java.util.HashSet; public class SetIntersectionExample { public static void main(String[] args) { Set<String> set1 = new HashSet<>(); set1.add("apple"); set1.add("banana"); set1.add("cherry"); Set<String> set2 = new HashSet<>(); set2.add("banana"); set2.add("date"); set2.add("cherry"); set1.retainAll(set2); // set1 now contains only elements also in set2 System.out.println("Intersection: " + set1); } }
Output
Intersection: [banana, cherry]
Common Pitfalls
Modifying the original set: The retainAll() method changes the set it is called on. If you want to keep the original sets unchanged, create a copy first.
Wrong approach example: Calling retainAll() directly on a set you want to keep intact will lose data.
Correct approach example: Use a new set to store the intersection.
java
import java.util.Set; import java.util.HashSet; public class IntersectionPitfall { public static void main(String[] args) { Set<String> set1 = new HashSet<>(); set1.add("a"); set1.add("b"); Set<String> set2 = new HashSet<>(); set2.add("b"); set2.add("c"); // Wrong: modifies set1 directly set1.retainAll(set2); System.out.println("Modified set1: " + set1); // Correct: create a copy to keep original sets Set<String> set3 = new HashSet<>(set1); Set<String> set4 = new HashSet<>(set2); Set<String> intersection = new HashSet<>(set3); intersection.retainAll(set4); System.out.println("Intersection without modifying originals: " + intersection); } }
Output
Modified set1: [b]
Intersection without modifying originals: [b]
Quick Reference
Use this quick guide to remember how to find the intersection of two sets in Java:
| Step | Action | Description |
|---|---|---|
| 1 | Create sets | Use HashSet or other Set implementations. |
| 2 | Copy set if needed | Create a new set if you want to keep originals unchanged. |
| 3 | Call retainAll() | Pass the other set to keep only common elements. |
| 4 | Use the result | The set now contains the intersection. |
Key Takeaways
Use
retainAll() to find the intersection of two sets in Java.Calling
retainAll() modifies the set it is called on, so copy it first if you want to keep the original.The intersection set contains only elements present in both sets.
Use
HashSet or any Set implementation to work with sets.Remember to import
java.util.Set and java.util.HashSet.