Java tutorial
//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); } } for (E entryB : b) { if (a.contains(entryB)) { intersect.add(entryB); } } return intersect; } }