Java-8新特性-通过Stream获取两个List复杂对象的交并差集

思路:首先获取两个list的id,通过id比较获取id的交并差集,再通过一次获取list对象里面的交并差集元素

代码直接可运行,个人觉得java8的stream非常类似ES6的集合运算,filter、foreach、map、reduce基本可以一一对应

代码:

package com.stream;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 *
 * @ClassName: TwoListCopare
 * @Description: 两个List<对象>取交集\并集\差集
 **/
public class TwoListCopare {
    public static void main(String[] args) {
        UserDTO userOld1 = new UserDTO("1","aaa",22);
        UserDTO userOld2 = new UserDTO("2","bbb",32);
        UserDTO userOld3 = new UserDTO("3","ccc",11);
        UserDTO userOld4 = new UserDTO("4","ddd",42);
        UserDTO userOld5 = new UserDTO("5","bbb",22);
        UserDTO userOld6 = new UserDTO("6","eee",24);

        UserDTO userNew1 = new UserDTO("7","dada",22);     //新增一个
        UserDTO userNew2 = new UserDTO("2","bbb",32);     //不变一个
        UserDTO userNew3 = new UserDTO("3","kaka",33);     //更新一个
        UserDTO userNew4 = new UserDTO("8","dbdb",42);     //新增一个
        UserDTO userNew5 = new UserDTO("5","bbb",100);     //更新一个
        //当然,少了1,4,6

        List mapAdd = new ArrayList<>();


        List oldList = new ArrayList<>();
        List newList = new ArrayList<>();

        //添加老数据
        oldList.add(userOld1);
        oldList.add(userOld2);
        oldList.add(userOld3);
        oldList.add(userOld4);
        oldList.add(userOld5);
        oldList.add(userOld6);
        //添加新数据
        newList.add(userNew1);
        newList.add(userNew2);
        newList.add(userNew3);
        newList.add(userNew4);
        newList.add(userNew5);

        //去交集,既获取id相同的交集,需要更新
        //1.先提取出id和结果,用map形式
        List oldIds = new ArrayList<>();
        List newIds = new ArrayList<>();
        oldList.stream().forEach(it->oldIds.add(it.getId()));
        newList.stream().forEach(it->newIds.add(it.getId()));
//        oldIds.stream().forEach(System.out::println);
//        newIds.stream().forEach(System.out::println);

        //取交集id
        System.out.println("-----------------交集----------------------");
        List collectUpdate = newIds.stream().filter(it -> oldIds.contains(it)).collect(Collectors.toList());
        collectUpdate.stream().forEach(System.out::println);
        //取对应交集的对象
        System.out.println("------------------交集的对象---------------------");
        List userUpdate = newList.stream().filter(it -> collectUpdate.contains(it.getId())).collect(Collectors.toList());
        userUpdate.stream().forEach(System.out::println);

        //取old的差集
        System.out.println("-----------------old的差集----------------------");
        List collectDelete = oldIds.stream().filter(it -> !newIds.contains(it)).collect(Collectors.toList());
        collectDelete.stream().forEach((System.out::println));
        //取对应old差集对象
        System.out.println("-----------------old差集对象----------------------");
        List userDelete = oldList.stream().filter(it -> collectDelete.contains(it.getId())).collect(Collectors.toList());
        userDelete.stream().forEach(System.out::println);

        //取new的差集
        System.out.println("-----------------new的差集----------------------");
        List collectAdd = newIds.stream().filter(it -> !oldIds.contains(it)).collect(Collectors.toList());
        collectAdd.stream().forEach((System.out::println));
        //取对应old差集对象
        System.out.println("-------------------old差集对象--------------------");
        List userAdd = newList.stream().filter(it -> collectAdd.contains(it.getId())).collect(Collectors.toList());
        userAdd.stream().forEach(System.out::println);


        //取并集
        System.out.println("-------------------并集--------------------");
        List allIds = new ArrayList<>();
        //获取一个包含了oldIds和newIds的总结合,但是没有去重
        allIds.addAll(oldIds);
        allIds.addAll(newIds);
        //去重,获取并集ids的新集合
        List joinIds = allIds.stream().distinct().collect(Collectors.toList());
        joinIds.stream().forEach(System.out::println);
    }
}

 结果:

Java-8新特性-通过Stream获取两个List复杂对象的交并差集_第1张图片 

ps:并集对象集合并没有写,因为能够得到判断id自然就能得到了

你可能感兴趣的:(后端)