Here you can find the source of intersect(Set
public static <E> Set<E> intersect(Set<E> a, Set<E> b)
//package com.java2s; import java.util.*; public class Main { public static <E> Set<E> intersect(Set<E> a, Set<E> b) { Set<E> intersect = new HashSet<>(); for (E entryA : a) { if (b.contains(entryA)) { intersect.add(entryA);// ww w . j a v a 2s. c om } } for (E entryB : b) { if (a.contains(entryB)) { intersect.add(entryB); } } return intersect; } }