Java tutorial
//package com.java2s; /* * Java Common Library * Copyright (c) 2008 BitCtrl Systems GmbH * * This library is free software; you can redistribute it and/or modify it under * the terms of the GNU Lesser General Public License as published by the Free * Software Foundation; either version 3.0 of the License, or (at your option) * any later version. * * This library is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more * details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * * Contact Information: * BitCtrl Systems GmbH * Weienfelser Strae 67 * 04229 Leipzig * Phone: +49 341-490670 * mailto: info@bitctrl.de */ import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; public class Main { public static <T> List<T> intersection(final List<T> list1, final List<T> list2) { final List<T> result = new ArrayList<T>(); for (final T e : list1) { if (list2.contains(e)) { result.add(e); } } return result; } /** * Bestimmt die Schnittmenge zweier Mengen. * * @param <T> * der Typ der Mengen. * @param set1 * die erste Menge. * @param set2 * die zweite Menge. * @return die Schnittmenge. */ public static <T> Set<T> intersection(final Set<T> set1, final Set<T> set2) { final Set<T> result = new HashSet<T>(); for (final T e : set1) { if (set2.contains(e)) { result.add(e); } } return result; } }