Have a look to the Set Interface.
According to the above website:
s1.addAll(s2) — transforms s1 into the union of s1 and s2. (The union
of two sets is the set containing all of the elements contained in
either set.)
s1.retainAll(s2) — transforms s1 into the intersection of
s1 and s2. (The intersection of two sets is the set containing only
the elements common to both sets.)
Here is an example:
import java.util.HashSet;
import java.util.Set;
public class Intersection
{
public static void main(String[] args)
{
Set<String> s1 = new HashSet<String>();
Set<String> s2 = new HashSet<String>();
s1.add("a");
s1.add("c");
s2.add("b");
s2.add("c");
s1.retainAll(s2);
System.out.println(s1);
}
}
The output of the above is:
[c]