Possible Duplicate:
Efficiently finding the intersection of a variable number of sets of strings
Say, have two Hashset, how to calculate the intersection of them?
Set<String> s1 = new HashSet<String>();
Set<String> s2 = new HashSet<String>();
S1 INT S2 ?
Yes there is
retainAll
check out thisUse the
retainAll()
method ofSet
:If you want to preserve the sets, create a new set to hold the intersection:
The javadoc of
retainAll()
says it's exactly what you want: