how to find intersection of two sets java code example
Example 1: get intersection of two lists java
Set<String> result = list.stream() .distinct() .filter(otherList::contains) .collect(Collectors.toSet()); Set<String> commonElements = new HashSet(Arrays.asList("red", "green")); Assert.assertEquals(commonElements, result);
Example 2: set intersection java
set1.retainAll(set2);