1. 程式人生 > >java Android 判斷兩個集合是否包含不同元素

java Android 判斷兩個集合是否包含不同元素

// 找出不同元素
private boolean getDiffrent(ArrayList<String> list1, ArrayList<String> list2) {
long st = System.nanoTime();
Map<String, Integer> map = new HashMap<String, Integer>(list1.size()
+ list2.size());
ArrayList<String> diff = new ArrayList<String>();
for (String string : list1) {
map.put(string, 1);
}
for (String string : list2) {
Integer cc = map.get(string);
if (cc != null) {
map.put(string, ++cc);
continue;
}
map.put(string, 1);
}
for (Map.Entry<String, Integer> entry : map.entrySet()) {
if (entry.getValue() == 1) {
diff.add(entry.getKey());
}
}
if (diff.size() == 0) {
flag = true;
}
return flag;
}