Java Commons Collections removeAll [英] Java Commons Collections removeAll

查看:556
本文介绍了Java Commons Collections removeAll的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

CollectionUtils :: removeAll()Commons Collections 3.2.1



我必须疯了,因为看起来这个方法是做什么的文档状态:


删除从集合中移除的元素。也就是说,此方法返回一个包含c中所有不在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);

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

失败



< blockquote>

java.lang.AssertionError:expected:< 2>但是:< 1>


并打印

 集合:[foo,bar,qux] 
remove:[foo]
result :[foo]



从我阅读的文档,我应该期望 [bar ,qux]

解决方案

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



链接到 CollectionUtils.java



有问题的行(1688 - 1691),并确认该方法之前已损坏:

  / * 
。 ..
* @since 4.0(方法存在于3.2,但是完全折断)
* /
public static< E>集合< E> removeAll(final Collection< E> collection,final Collection<?> remove){
return ListUtils.removeAll(collection,remove);
}








不,你不疯了。 removeAll()实际上(不正确)调用 retainAll()



这是 CollectionUtils 中的一个错误,影响版本3.2。已修复,但只在4.0分支中。



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



作为进一步的证明,这里有一个指向源代码的链接: / p>

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



查看此行:

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

是的...破碎!


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?

解决方案

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!

这篇关于Java Commons Collections removeAll的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆