java字符串集合一致性比较


public Map getDifferList(List listA ,List listB){
    Map> returnMap = new HashMap();    //返回结果
    List differAList = new ArrayList<>();          //A有B没有
    List differBList = new ArrayList<>();          //B有A没有
    List sameList = new ArrayList<>();             //相同元素
    
    Map 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 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

你可能感兴趣的:(java,windows,开发语言)