private static List<String> getDiffrent5(List<String> list1, List<String> list2) {
long st = System.nanoTime();
List<String> maxdiff = new ArrayList<String>();
List<String> mixdiff = new ArrayList<String>();
List<String> allSame = new ArrayList<String>();
//遍历较大的list
List<String> maxList = list1;
List<String> minList = list2;
if(list2.size()>list1.size())
{
maxList = list2;
minList = list1;
}
Map<String,Integer> map = new HashMap<String,Integer>(maxList.size());
for (String string : maxList) {
map.put(string, 1);
}
for (String string : minList) {
if(map.get(string)!=null)
{
map.put(string, 2);
}else{
map.put(string, -1);
}
}
for(Map.Entry<String, Integer> entry:map.entrySet())
{
if(entry.getValue()==1)
{
maxdiff.add(entry.getKey()); //获取maxList中含有mixList中没有的
}else if(entry.getValue()==-1){
mixdiff.add(string); //获取minList中含有maxList中没有的
}else if(entry.getValue()==2){
allSame.add(string); //获取minList与maxList中共有的数据
}
}
System.out.println("getDiffrent5 total times "+(System.nanoTime()-st)); //计算用时
List allList = new ArrryList();
allList.add(maxdiff);
allList.add(mixdiff);
allList.add(allSame);
return diff;
}
快速分辨两个不同大小list中相同以及各不同的数据
最新推荐文章于 2023-11-01 07:04:56 发布