Java tutorial
//package com.java2s; /* * Copyright 2001-2008 Aqris Software AS. All rights reserved. * * This program is dual-licensed under both the Common Development * and Distribution License ("CDDL") and the GNU General Public * License ("GPL"). You may elect to use one or the other of these * licenses. */ import java.util.ArrayList; import java.util.Comparator; import java.util.List; public class Main { public static List intersection(List list1, List list2) { List result = new ArrayList(list1.size() + list2.size()); Object item; for (int i = 0, max_i = list1.size(); i < max_i; i++) { item = list1.get(i); if (list2.contains(item)) { result.add(item); } } return result; } /** * Uses given comparator to compare all list items to the given object to see * if there are some equal objects in the list. * * @return true if there is at least one equal object, false otherwise */ public static final boolean contains(List list, Object object, Comparator comparator) { for (int i = 0; i < list.size(); i++) { if (comparator.compare(list.get(i), object) == 0) { return true; } } return false; } }