From da5a378c4fd1757393fa599f7e6766e48dd83ce2 Mon Sep 17 00:00:00 2001 From: Henri Yandell Date: Sun, 18 Jul 2010 08:07:36 +0000 Subject: [PATCH] Applying Thomas Rogan's patch to COLLECTIONS-328, improving the performance to ListUtils.intersection in the manner described by Jilles van Gurp git-svn-id: https://svn.apache.org/repos/asf/commons/proper/collections/trunk@965176 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/commons/collections/ListUtils.java | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/java/org/apache/commons/collections/ListUtils.java b/src/java/org/apache/commons/collections/ListUtils.java index c9197d237..f67ca1c26 100644 --- a/src/java/org/apache/commons/collections/ListUtils.java +++ b/src/java/org/apache/commons/collections/ListUtils.java @@ -19,6 +19,7 @@ package org.apache.commons.collections; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; +import java.util.HashSet; import java.util.Iterator; import java.util.List; @@ -70,12 +71,20 @@ public class ListUtils { */ public static List intersection(final List list1, final List list2) { final List result = new ArrayList(); - final ArrayList copyOfList1 = new ArrayList(list1); - for (E e : list2) { - if (copyOfList1.contains(e)) { + List smaller = list1; + List larger = list2; + if (list1.size() > list2.size()) { + smaller = list2; + larger = list1; + } + + HashSet hashSet = new HashSet(smaller); + + for (E e : larger) { + if (hashSet.contains(e)) { result.add(e); - copyOfList1.remove(e); + hashSet.remove(e); } } return result;