Java Commons Collections removeAll

markdsievers picture markdsievers · Dec 4, 2011 · Viewed 9.7k times · Source

CollectionUtils::removeAll() Commons Collections 3.2.1

I must be going crazy, becuase it seems like this method is doing the inverse of what the docs state:

Removes the elements in remove from collection. That is, this method returns a collection containing all the elements in c that are not in remove.

This little JUnit test

@Test
public void testCommonsRemoveAll() throws Exception {
    String str1 = "foo";
    String str2 = "bar";
    String str3 = "qux";

    List<String> collection = Arrays.asList(str1, str2, str3);
    System.out.println("collection: " + collection);

    List<String> remove = Arrays.asList(str1);
    System.out.println("remove: " + remove);

    Collection result = CollectionUtils.removeAll(collection, remove);
    System.out.println("result: " + result);
    assertEquals(2, result.size());
}

Is failing with

java.lang.AssertionError: expected:<2> but was:<1>

and prints

collection: [foo, bar, qux] 
remove: [foo] 
result: [foo]

From my reading of the docs I should expect [bar, qux]. What have I missed?

Answer

逆さま picture 逆さま · Dec 4, 2011

Edit January 1, 2014 Apache Commons Collections 4.0 was finally released on November 21, 2013, and contains a fix for this issue.

Link to CollectionUtils.java

Lines in question (1688 - 1691), with acknowledgement the method was previously broken:

/*
 ...
 * @since 4.0 (method existed in 3.2 but was completely broken)
 */
public static <E> Collection<E> removeAll(final Collection<E> collection, final Collection<?> remove) {
    return ListUtils.removeAll(collection, remove);
}

Original Answer

Nope, you're not crazy. removeAll() is actually (incorrectly) calling retainAll().

This is a bug in CollectionUtils, affecting version 3.2. It's been fixed, but only in the 4.0 branch.

https://issues.apache.org/jira/browse/COLLECTIONS-349

And as further proof, here's a link to the source code:

http://svn.apache.org/repos/asf/commons/proper/collections/tags/COLLECTIONS_3_2/src/java/org/apache/commons/collections/CollectionUtils.java

Check out this line:

public static Collection removeAll(Collection collection, Collection remove) {
    return ListUtils.retainAll(collection, remove);
}

Yep...broken!