2011-12-04 45 views
26

CollectionUtils::removeAll() Commons Collections 3.2.1的Java Commons Collections中的removeAll

我必须要疯了,监守好像这种方法是做什么的文档状态反:

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.

这一点JUnit测试

@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()); 
} 

正在失败

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

,并打印

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

从我,我应该想到[bar, qux]的文档阅读。我错过了什么?

+0

我更新了我的文章以反映这一点,因为有人让我想起了它 - 但Apache Commons Collections 4.0于2013年11月发布,并为此问题提供了修复。 – birryree

回答

34

编辑2014年1月1日 Apache Commons Collections 4.0终于在2013年11月21日发布,并且包含此问题的修复。

Link to CollectionUtils.java

线有问题(1688年至1691年),以确认该方法之前被打破:

/* 
... 
* @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); 
} 

原来的答案

不,你不是疯。 removeAll()实际上是(错误地)呼叫retainAll()

这是CollectionUtils中的一个bug,影响版本3.2。它已被修复,但仅限于4.0分支。

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

而且进一步证明,这里是一个链接到源代码:

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

退房这一行:

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

是的...碎了!

+1

神圣烟!它是如何穿过裂缝的?谢谢(你的)信息。 Upvote并接受你。 – markdsievers

+0

@markdsievers - 看起来像单元测试是需要的,或需要修复! – birryree

+0

海事组织,这是相当差。错误是可以的,但是最初的问题有一个“02/Aug/06 17:37”的创建标记,他们仍然没有发布修正版的产品。 –

相关问题