Java examples for java.util:Collection Operation
set Intersection for Collection
/******************************************************************************* * Copyright (c) 2004, 2007 Boeing./*from w w w . j ava 2 s. co m*/ * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * Boeing - initial API and implementation *******************************************************************************/ //package com.java2s; import java.util.ArrayList; import java.util.Collection; import java.util.List; public class Main { public static void main(String[] argv) { Collection listA = java.util.Arrays.asList("asdf", "java2s.com"); Collection listB = java.util.Arrays.asList("asdf", "java2s.com"); System.out.println(setIntersection(listA, listB)); } /** * @return The intersection of two sets A and B is the set of elements common to A and B */ public static <T> List<T> setIntersection(Collection<T> listA, Collection<T> listB) { ArrayList<T> intersection = new ArrayList<T>(listA.size()); for (T obj : listA) { if (listB.contains(obj)) { intersection.add(obj); } } return intersection; } }