Author: bayard
Date: Sun Jul 18 08:07:36 2010
New Revision: 965176
URL: http://svn.apache.org/viewvc?rev=965176&view=rev
Log:
Applying Thomas Rogan's patch to COLLECTIONS-328, improving the performance to
ListUtils.intersection in the manner described by Jilles van Gurp
Modified:
commons/proper/collections/trunk/src/java/org/apache/commons/collections/ListUtils.java
Modified:
commons/proper/collections/trunk/src/java/org/apache/commons/collections/ListUtils.java
URL:
http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/java/org/apache/commons/collections/ListUtils.java?rev=965176&r1=965175&r2=965176&view=diff
==============================================================================
---
commons/proper/collections/trunk/src/java/org/apache/commons/collections/ListUtils.java
(original)
+++
commons/proper/collections/trunk/src/java/org/apache/commons/collections/ListUtils.java
Sun Jul 18 08:07:36 2010
@@ -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 <E> List<E> intersection(final List<? extends E> list1,
final List<? extends E> list2) {
final List<E> result = new ArrayList<E>();
- final ArrayList<E> copyOfList1 = new ArrayList<E>(list1);
- for (E e : list2) {
- if (copyOfList1.contains(e)) {
+ List<? extends E> smaller = list1;
+ List<? extends E> larger = list2;
+ if (list1.size() > list2.size()) {
+ smaller = list2;
+ larger = list1;
+ }
+
+ HashSet<E> hashSet = new HashSet<E>(smaller);
+
+ for (E e : larger) {
+ if (hashSet.contains(e)) {
result.add(e);
- copyOfList1.remove(e);
+ hashSet.remove(e);
}
}
return result;