Here you can find the source of intersect(Collection
public static <T> Collection<T> intersect(Collection<T> set1, Collection<T> set2)
//package com.java2s; //License from project: Apache License import java.util.Collection; import java.util.Collections; import java.util.LinkedHashSet; import java.util.Map; import java.util.Set; public class Main { public static <T> Collection<T> intersect(Collection<T> set1, Collection<T> set2) { if (isEmpty(set1) || isEmpty(set2)) { return Collections.emptyList(); }//from www .j a v a2s .co m Set<T> intersection = new LinkedHashSet<T>(); for (T item : set1) { if (item != null && set2.contains(item)) { intersection.add(item); } } return intersection; } public static boolean isEmpty(Collection<? extends Object> col) { return !isNotEmpty(col); } public static boolean isEmpty(Map<? extends Object, ? extends Object> map) { return map == null || map.isEmpty(); } public static boolean isNotEmpty(Collection<? extends Object> col) { return col != null && col.size() > 0; } public static boolean isNotEmpty(Map<? extends Object, ? extends Object> map) { return map != null && map.size() > 0; } }