import java.util.HashSet;
import java.util.Set;
public class Main {
public static void main(String[] args) {
Set<Integer> set1 = new HashSet<>();
set1.add(1);
set1.add(3);
set1.add(2);
set1.add(4);
set1.add(8);
set1.add(9);
set1.add(0);
Set<Integer> set2 = new HashSet<>();
set2.add(1);
set2.add(3);
set2.add(7);
set2.add(5);
set2.add(4);
set2.add(0);
set2.add(7);
set2.add(5);
// intersection
Set<Integer> setIntersection = new HashSet<>(set1);
setIntersection.retainAll(set2);
System.out.println(setIntersection);
}
}
// Output: [0, 1, 3, 4]
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter