public static void main(String[] args) {
Collection collection = new ArrayList();
Collection remove = new ArrayList();
Collection retain = new ArrayList();
collection.add(new String("A"));
collection.add(new String("B"));
collection.add(new String("C"));
collection.add(new String("D"));
remove.add(new String("A"));
remove.add(new String("B"));
remove.add(new String("C"));
retain.add(new String("A"));
retain.add(new String("D"));
retain.add(new String("E"));
Collection result = (Collection)CollectionUtils.removeAll(collection, remove);
System.out.println(result.size());
for (String string : result) {
System.out.print(string);
System.out.print(",");
}
System.out.println();
result = (Collection)CollectionUtils.retainAll(collection, retain);
System.out.println(result.size());
for (String string : result) {
System.out.print(string);
System.out.print(",");
}
}
输出结果:
3
A,B,C,
2
A,D,
实际上removeAll 应该返回一个包含字符串 D 的Collection才对,这里应该是CollectionUtils的一个bug
——————————————————————————————————————————————————
分享到:
2008-11-19 11:04
浏览 2656
评论
1 楼
zhuuk999
2009-02-23
不是BUG吧,doc上写的很清楚a Collection containing all the elements of collection except any elements that also occur in remove.
要返回D 的Collectiony应该用subtract才对