JAVA List 获取两个集合的交集 并集 差集

2023-05-16

public class ExtractIdUtils {


    public static Map<String, List<Long>> extractList(List<Long> newIds, List<Long> oldIds) {
        List<Long> tempList = new ArrayList<>(oldIds);
        //取交集
        tempList.retainAll(newIds);
        //删除
        oldIds.removeAll(tempList);
        //新增
        newIds.removeAll(tempList);
        Map<String, List<Long>> extractMap = new HashMap<>();
        extractMap.put("del", oldIds);
        extractMap.put("add", newIds);
        extractMap.put("update", tempList);

        return extractMap;
    }


    public static void main(String[] args) {
        ArrayList<Long> newIds = new ArrayList<>();
        newIds.add(1L);
        newIds.add(2L);
        newIds.add(3L);
        newIds.add(5L);
        ArrayList<Long> oldIds = new ArrayList<>();
        oldIds.add(1L);
        oldIds.add(2L);
        oldIds.add(3L);
        oldIds.add(4L);
        Map<String, List<Long>> map = extractList(newIds, oldIds);
        List<Long> del = map.get("del");
        List<Long> add = map.get("add");
        List<Long> update = map.get("update");


        System.out.println("del………"+del);
        System.out.println("add………"+add);
        System.out.println("update………"+update);

    }
}

 

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

JAVA List 获取两个集合的交集 并集 差集 的相关文章

随机推荐