Here you can find the source of intersection(List
public static <T> Set<T> intersection(List<Collection<? extends T>> sets)
//package com.java2s; //License from project: Open Source License import java.util.*; public class Main { public static <T> Set<T> intersection(List<Collection<? extends T>> sets) { if (sets.isEmpty()) return Collections.emptySet(); Set<T> accum = new HashSet<>(); accum.addAll(sets.get(0));/*from ww w . j ava2s . c om*/ for (int i = 1; i < sets.size(); i++) { accum = intersection(accum, sets.get(i)); } return accum; } public static <T> Set<T> intersection(Collection<? extends T> lhs, Collection<? extends T> rhs) { Collection<? extends T> minSet = lhs.size() < rhs.size() ? lhs : rhs; Collection<? extends T> maxSet = lhs.size() < rhs.size() ? rhs : lhs; HashSet<T> isect = new HashSet<>(); for (T x : minSet) { if (maxSet.contains(x)) { isect.add(x); } } return isect; } }