Applying Mark Shead's patch to COLLECTIONS-359. The intersection method was not handling duplicates correctly.

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/collections/trunk@965173 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Henri Yandell 2010-07-18 07:43:15 +00:00
parent ca34318ae2
commit a11dc6aa1d
2 changed files with 18 additions and 1 deletions

View File

@ -70,10 +70,12 @@ 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 (list1.contains(e)) {
if (copyOfList1.contains(e)) {
result.add(e);
copyOfList1.remove(e);
}
}
return result;

View File

@ -110,6 +110,21 @@ public class TestListUtils extends BulkTest {
assertEquals(fullList, ListUtils.intersection(fullList, fullList));
}
/**
* Tests intersecting two lists in different orders.
*/
public void testIntersectionOrderInsensitivity() {
List one = new ArrayList();
List two = new ArrayList();
one.add("a");
one.add("b");
two.add("a");
two.add("a");
two.add("b");
two.add("b");
assertEquals(ListUtils.intersection(one,two),ListUtils.intersection(two, one));
}
public void testPredicatedList() {
Predicate<Object> predicate = new Predicate<Object>() {
public boolean evaluate(Object o) {