【发布时间】:2012-01-12 10:35:45
【问题描述】:
CollectionUtils::removeAll() Commons Collections 3.2.1
我一定要疯了,因为这种方法似乎与文档状态相反:
从集合中移除 remove 中的元素。也就是说,此方法返回一个集合,其中包含 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);
Collection result = CollectionUtils.removeAll(collection, remove);
System.out.println("result: " + result);
assertEquals(2, result.size());
}
失败了
java.lang.AssertionError: 预期: 但是是:
和打印
collection: [foo, bar, qux]
remove: [foo]
result: [foo]
根据我对文档的阅读,我应该期待 [bar, qux]。我错过了什么?
【问题讨论】:
-
我更新了我的帖子以反映这一点,因为有人提醒了我 - 但 Apache Commons Collections 4.0 于 2013 年 11 月发布,修复了这个问题。
标签: java collections apache-commons-collection