Java tutorial
//package com.java2s; import java.util.*; public class Main { public static <T extends Object> int countIntersection(Collection<? extends T> s1, Collection<? extends T> s2) { if (s1 == null || s2 == null) return 0; int n1 = s1.size(); int n2 = s2.size(); if (n2 < n1) { Collection<? extends T> t = s1; s1 = s2; s2 = t; } int n = 0; for (T e : s1) { if (s2.contains(e)) n++; } return n; } public static int size(Collection<?> c) { return (c == null) ? 0 : c.size(); } }