public static void main(String[] args) {
Collection<String> collection = new ArrayList<String>();
Collection<String> remove = new ArrayList<String>();
Collection<String> retain = new ArrayList<String>();
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<String> result = (Collection<String>)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<String>)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
——————————————————————————————————————————————————