Here you can find the source of intersection(Set
Parameter | Description |
---|---|
sa | a set |
sb | anotehr set |
sa
and sb
public static <E> Set<E> intersection(Set<E> sa, Set<E> sb)
//package com.java2s; //License from project: Apache License import java.util.*; public class Main { /**//w ww . j a v a2s. co m * @param sa a set * @param sb anotehr set * @return the intersection of <code>sa</code> and <code>sb</code> */ public static <E> Set<E> intersection(Set<E> sa, Set<E> sb) { Set<E> result = new HashSet<E>(); if (sa == null || sb == null || sa.isEmpty() || sb.isEmpty()) return result; Set<E> smallest = sa.size() < sb.size() ? sa : sb; Set<E> biggest = smallest == sa ? sb : sa; for (E entry : smallest) { if (biggest.contains(entry)) result.add(entry); } return result; } }