Java examples for java.util:Set Intersection
Write code to Returns the intersection of two sets.
//package com.java2s; import java.util.HashSet; import java.util.Set; public class Main { /**//www . j a v a 2 s . c om * Returns the intersection of two sets. It calls {@link Set#retainAll(java.util.Collection)} but returns a new set * containing the elements of the intersection. * * @param set1 * the first set * @param set2 * the second set * @return the intersection of the sets */ public static <V> Set<V> intersection(Set<V> set1, Set<V> set2) { Set<V> intersection = new HashSet<V>(); intersection.addAll(set1); intersection.retainAll(set2); return intersection; } }