Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6887229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:51:34+00:00 2026-05-27T05:51:34+00:00

CollectionUtils::removeAll() Commons Collections 3.2.1 I must be going crazy, becuase it seems like this

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T05:51:35+00:00Added an answer on May 27, 2026 at 5:51 am

    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!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to use org.apache.commons.collections.CollectionUtils in android with following code import java.util.ArrayList; import
I have a class hierarchy like this: @Entity @Table (name=call_distribution_policies) @Inheritance (strategy=InheritanceType.JOINED) public class
When I've used common-collections I've done custom extensions to those utils like: class MyCollectionUtils
I want to compute differences between collections. When using CollectionUtils.subtract() for custom comparison I
Very often I need something like that: foreach (Line line in lines) { if
I'm doing a POC to push Data from a (Java) server, though LCDS 3.1
I've read some post comparing Guava and Apache Commons, and most of the posters
I would like to make use @IndexColumn to set seq number of some data
I have a highly dynamic single page interface where various user events trigger new
I've got an existing ArrayList which will be filtered according to certain criteria. I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.