Java examples for java.util:Collection Intersect
Returns intersection of two collections.
//package com.java2s; import java.util.Collection; import java.util.HashSet; import java.util.Set; public class Main { public static void main(String[] argv) { Collection coll1 = java.util.Arrays.asList("asdf", "java2s.com"); Collection coll2 = java.util.Arrays.asList("asdf", "java2s.com"); System.out.println(intersect(coll1, coll2)); }//from ww w .ja v a 2 s.c om /** * Returns intersection of two collections. * * @param <T> the generic type * @param coll1 the coll1 * @param coll2 the coll2 * @return the collection */ public static <T> Collection<T> intersect(Collection<T> coll1, Collection<T> coll2) { Set<T> intersection = new HashSet<T>(coll1); intersection.retainAll(new HashSet<T>(coll2)); return intersection; } }