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:
parent
ca34318ae2
commit
a11dc6aa1d
|
@ -70,10 +70,12 @@ public class ListUtils {
|
||||||
*/
|
*/
|
||||||
public static <E> List<E> intersection(final List<? extends E> list1, final List<? extends E> list2) {
|
public static <E> List<E> intersection(final List<? extends E> list1, final List<? extends E> list2) {
|
||||||
final List<E> result = new ArrayList<E>();
|
final List<E> result = new ArrayList<E>();
|
||||||
|
final ArrayList<E> copyOfList1 = new ArrayList<E>(list1);
|
||||||
|
|
||||||
for (E e : list2) {
|
for (E e : list2) {
|
||||||
if (list1.contains(e)) {
|
if (copyOfList1.contains(e)) {
|
||||||
result.add(e);
|
result.add(e);
|
||||||
|
copyOfList1.remove(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
|
|
|
@ -110,6 +110,21 @@ public class TestListUtils extends BulkTest {
|
||||||
assertEquals(fullList, ListUtils.intersection(fullList, fullList));
|
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() {
|
public void testPredicatedList() {
|
||||||
Predicate<Object> predicate = new Predicate<Object>() {
|
Predicate<Object> predicate = new Predicate<Object>() {
|
||||||
public boolean evaluate(Object o) {
|
public boolean evaluate(Object o) {
|
||||||
|
|
Loading…
Reference in New Issue