CollectionUtils::removeAll() Commons Collections 3.2.1Java Commons Collections RemoveAll
tôi phải phát điên, bởi vì nó có vẻ như phương pháp này là làm ngược lại những gì các tài liệu nhà nước:
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 nhỏ này kiểm tra
@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());
}
Lỗi với
java.lang.AssertionError: expected:<2> but was:<1>
và in
collection: [foo, bar, qux]
remove: [foo]
result: [foo]
Từ đọc sách của tôi trong những tài liệu tôi nên mong đợi [bar, qux]
. Tôi đã bỏ lỡ điều gì?
Tôi đã cập nhật bài đăng của mình để phản ánh điều này vì ai đó đã nhắc tôi nhớ - nhưng Bộ sưu tập của Apache Commons 4.0 đã được phát hành vào tháng 11 năm 2013, với bản sửa lỗi cho vấn đề này. – birryree