public Map<String,List<String> getDifferList(List<String> listA ,List<String> listB){Map<String,List<String>> returnMap = new HashMap(); //返回结果List<String> differAList = new ArrayList<>(); //A有B没有List<String> differBList = new ArrayList<>(); //B有A没有List<String> sameList = new ArrayList<>(); //相同元素Map<String,Integer> mapA = new HashMap<>();for (String strA : listA){mapA.put(strA,1)}for (String strB : listB){Integer value = mapA.get(strB);if(value != null){ //相同元素mapA.put(strB, ++value);sameList.add(strB);continue;}differBList.add(strB); //B的不同点}for (Map.Entry<String, Integer> entry : mapA.entrySet()) {if (entry.getValue () == 1) {differAList.add(Entry.getKey()); //A的不同点}}returnMap.put("differAList", differAList);returnMap.put("differBList", differBList);returnMap.put("sameList", sameList);}
调用方法getDifferList()
假如传参listA 为a、b、c
listB为c、d、e
则输出结果为
differAList 为 a、b
differAList 为 d、e
sameList 为 c