Android 比较两个集合中的不同元素

2019-12-11  本文已影响0人  信仰_021e

private static List getDiffrent(List list1, List list2) {

List diff =new ArrayList();

long start = System.currentTimeMillis();

Map map =new HashMap(list1.size() + list2.size());

List maxList = list1;

List minList = list2;

if (list2.size() > list1.size()) {

maxList = list2;

minList = list1;

}

for (String string : maxList) {

map.put(string,1);

}

for (String string : minList) {

Integer count = map.get(string);

if (count !=null) {

map.put(string, ++count);

continue;

}

map.put(string,1);

}

for (Map.Entry entry : map.entrySet()) {

if (entry.getValue() ==1) {

diff.add(entry.getKey());

}

}

System.out.println("耗时:" + (System.currentTimeMillis() - start) +" 毫秒");

return diff;

}

上一篇下一篇

猜你喜欢

热点阅读