找出两个String集合中不同的元素
public static List<String> getDifferListByMapPlus(Collection<String> collmax, Collection<String> collmin) {
Collection csReturn = new LinkedList();
Collection max = collmax;
Collection min = collmin;
long beginTime = System.currentTimeMillis();
if (collmax.size() < collmin.size()) {
max = collmin;
min = collmax;
}
Map<Object, Integer> map = new HashMap<Object, Integer>(max.size());
for (Object object : max) {
map.put(object, 1);
}
for (Object object : min) {
if (map.get(object) == null) {
csReturn.add(object);
} else {
map.put(object, 2);
}
}
for (Map.Entry<Object, Integer> entry : map.entrySet()) {
if (entry.getValue() == 1) {
csReturn.add(entry.getKey());
}
}
long endTime = System.currentTimeMillis();
System.out.println("集合A和集合B不同的元素:"+csReturn);
System.out.println("使用map方式遍历, 对比耗时: " + (endTime - beginTime)+"毫秒。");
return new ArrayList<String>(csReturn);