获取差集数据(比较两个集合、获取需要新增或删除的数据)

1 篇文章 0 订阅

获取差集数据(比较两个集合、获取需要新增或删除的数据)

最近遇到一个企业微信对接需求 其中有一个 接口是 获取客户群详情 ,里面有群成员,由于每次都是给的全部,需要和数据库存的进行对比差异(多了或少了某个人)

	
	/**
	* 可直接复制运行
	* 
     /**
      *          oldData.add(new Student("a"));
      *          oldData.add(new Student("del"));
      *          oldData.add(new Student("del1"));
      *          oldData.add(new Student("del2"));
      *          
      *          newData.add(new Student("a"));
      *          newData.add(new Student("c"));
      *          newData.add(new Student("add"));
      *          newData.add(new Student("add11"));
      *          
      *          输出:以下是需要删除的数据2
      *               delData:del
      *               delData:del1
      *               delData:del2
      *               ===========
      *               以下是需要添加的数据2
      *               addData:c
      *               addData:add
      *               addData:add11
      */
	* 
	*/ 
     public static void main(String[] args) throws Exception {
 //        reversePrint();
         getDifferentData();
     }
     


     /**
      * 获取差集数据
      * 需要新增和删除的数据
      */
     
     public static void getDifferentData() throws Exception {
        List<Student >oldData=new ArrayList<>();
         for (int i=0;i<100000;i++){
             oldData.add(new Student("a"+i));
         }
         oldData.add(new Student("a"));
         oldData.add(new Student("del"));
         oldData.add(new Student("del1"));
         oldData.add(new Student("del2"));

         List<Student >newData=new ArrayList<>();

         for (int i=0;i<100000;i++){
             newData.add(new Student("a"+i));
         }
         newData.add(new Student("a"));
         newData.add(new Student("c"));
         newData.add(new Student("add"));
         newData.add(new Student("add11"));
//         List<Student >addList=new ArrayList<>(list2);

         long start=System.currentTimeMillis();
         //获取到需要删除的差集
         List<Student> delList = removeAll(oldData,newData,Student::getId);
          List<Student> addList= removeAll(newData,oldData,Student::getId);
         System.out.println("执行耗时:"+(System.currentTimeMillis()-start)+"ms");

         System.out.println("以下是需要删除的数据");
         for (Student s : delList) {
             System.out.println("delData:"+s.getId());
         }
         System.out.println("===========\n");

         System.out.println("以下是需要添加的数据");
         for (Student s : addList) {
             System.out.println("addData:"+s.getId());
         }

         System.out.println("===========到底了===========");
         
     }

     /**
      * 差集:删除左边集合中在右边集合存在的元素并返回
      * @param left
      * @param right
      * @return
      */
     public static <T,R> List<T> removeAll(List<T> left, List<T> right, Function<T, R> fun) throws Exception {
         if (left == null){
             return null;
         }
         if (right == null){
             return left;
         }
         Set<R> set = new HashSet<>();
         //使用LinkedList方便插入和删除
         List<T> res = new LinkedList<>(left);
         for(T item : right){
             set.add(fun.apply(item));
         }
         //如果set中包含id则remove
         res.removeIf(item -> set.contains(fun.apply(item)));
         return res;
     }



public static class Student  implements Serializable {
         private String id;
         private String name;
  

         public String getId() {
             return id;
         }

         public void setId(String id) {
             this.id = id;
         }

         public String getName() {
             return name;
         }

         public void setName(String name) {
             this.name = name;
         }

         public Student(String id) {
             this.id = id;
         }

         public Student() {
         }
     }

2021年7月2日 修改 不指定具体泛型,取消反射调用,感谢卢老师的帮助 @https://blog.csdn.net/weixin_43745366?spm=1001.2014.3001.5512

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值