CollectionUtils::removeAll() Commons Collections 3.2.1
我一定是要疯了,因为看起来这个方法正在做与文档状态相反的方法:
从集合中删除remove中的元素 . 也就是说,此方法返回一个集合,其中包含c中未删除的所有元素 .
这个小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>但是:<1>
和打印
collection: [foo, bar, qux]
remove: [foo]
result: [foo]
从我对文档的阅读中我应该期待 [bar, qux]
. 我错过了什么?
1 回答
d23545 Apache Commons Collections 4.0最终于2013年11月21日发布,其中包含针对此问题的修复程序 .
Link to CollectionUtils.java
有问题的行(1688 - 1691),确认该方法先前被破坏:
Original Answer
不,你不是疯了 .
removeAll()
实际上是(错误地)调用retainAll()
.这是
CollectionUtils
中的一个错误,影响版本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
看看这一行:
是的...碎了!