Here you can find the source of getIntersection(Set
public static <T> Set<T> getIntersection(Set<T> a, Set<T> b)
//package com.java2s; //License from project: Apache License import java.util.Collections; import java.util.HashSet; import java.util.Set; public class Main { public static <T> Set<T> getIntersection(Set<T> a, Set<T> b) { if (a == null || b == null) { return Collections.EMPTY_SET; }//from w w w. ja v a 2 s .c o m Set<T> small = (a.size() > b.size()) ? b : a; Set<T> big = (a.size() > b.size()) ? a : b; Set<T> intersection = new HashSet<T>(small); intersection.retainAll(big); return intersection; } }