具体如下:
有两个集合Collection a,Collection b,需要把b中与a中集合元素相同的剔除掉,只留下在a集合中不包含b的元素,测试方法如下:
@Test
public void testCollectionRemove() {
ArrayList<String> list = new ArrayList<String>();
list.add("11");
list.add("22");
list.add("33");
list.add("44");
ArrayList<String> list1 = new ArrayList<String>();
list1.add("22");
list1.add("44");
list1.add("55");
Collection c = CollectionUtils.removeAll(list, list1);
assertEquals(true, ((List) c).contains("11"));
assertEquals(true, ((List) c).contains("33"));
assertEquals(false, ((List) c).contains("22"));
assertEquals(false, ((List) c).contains("44"));
}
java.lang.AssertionError: expected:<true> but was:<false>
at org.junit.Assert.fail(Assert.java:74)
at org.junit.Assert.failNotEquals(Assert.java:448)
at org.junit.Assert.assertEquals(Assert.java:102)
at org.junit.Assert.assertEquals(Assert.java:117)
at com.gresoft.components.TestCommonsCollection.testCollectionRemove(TestCommonsCollection.java:90)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:59)
at org.junit.internal.runners.MethodRoadie.runTestMethod(MethodRoadie.java:98)
at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:79)
at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:87)
at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:77)
at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:42)
at org.junit.internal.runners.JUnit4ClassRunner.invokeTestMethod(JUnit4ClassRunner.java:88)
at org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:51)
at org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4ClassRunner.java:44)
at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:26)
at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:36)
at org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:42)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:38)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
在第一个assertEquals中junit抱应该是false,实际应该是true,查看源码发现:
public static Collection removeAll(Collection collection, Collection remove)
{
return ListUtils.retainAll(collection, remove);
}
public static List retainAll(Collection collection, Collection retain)
{
List list = new ArrayList(Math.min(collection.size(), retain.size()));
for(Iterator iter = collection.iterator(); iter.hasNext();)
{
Object obj = iter.next();
if(retain.contains(obj))
list.add(obj);
}
return list;
}
后来发现
CollectionUtils.removeAll(list, list1);和CollectionUtils.retainAll(list, list1);功能是一样的,所以这跟它文档是不符合的,大家在使用时候注意!