工具类:关于如何找到两个List数组中不同的数据的算法!

找到两个List数组中不同的数据的算法!

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GetListDifferent {

public static Boolean isDifferentWithList(String url, List<String> list){
return list.contains(url) ? false:true;
}

/**
* @param list1
* @param list2
* @return
*/
public static List<String> getDiffrent(List<String> list1, List<String> list2) {
long st = System.nanoTime();
Map<String,Integer> map = new HashMap<String,Integer>(list1.size()+list2.size());
List<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());
}
}
// System.out.println("getDiffrent3 total times "+(System.nanoTime()-st));
return diff;
}

}

数据结构是我们开发人员必须要学习的一门课程,他是程序设计的灵魂,软件开发的必备工具。本人不才,推荐一本关于数据结构的参考书籍 : 严蔚敏

数据结构题集《C语言版》  

原文地址:https://www.cnblogs.com/mageblog/p/7493946.html