zoukankan      html  css  js  c++  java
  • Java集合对象比对

    1. 场景描述

    通过java代码从外围接口中获取数据并落地,已经存在的不落地,不存在的落地,因有部分字段变化是正常的,只需比对3个字段相同即为相同。

    2. 解决方案

    设置定时任务(三个标签完成springboot定时任务配置),比对接口中获取的数据和本地落地的数据是否相同。

    2.1 真实代码

      @Scheduled(cron = "0 10 2 * * ? ")
        public void execAppUser() {
            synchronized (this) {
                JSONArray array = null;
                try {
                    array = testService.getUsers();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                //接口获取user
                List<AppUser> iusers = JSONObject.parseArray(array.toJSONString(), AppUser.class);
                
                List<AppUser> users = appUserService.getAllUsers();
                for (AppUser iuser : iusers) {
                    if (users.contains(iuser)){
                        continue;
                    }
                    appUserService.insert(iuser);
                }
            }
        }
    

    2.2 代码说明

    (1)使用users.contains(iuser),比对目前库中是否存在从接口获取的对象。

    (2)因只比对三个字段,三个字段相同即为相同,需重写AppUser对象的equals和hashcode

    (3)Idea中Alt+insert快捷键调出来,选择对应的字段就能生成对应的equals和hashCode。

    (4)equals代码

     @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            AppUser appUser = (AppUser) o;
            return Objects.equals(aa, appUser.aa) &&
                    Objects.equals(bb, appUser.bb) &&
                    Objects.equals(cc, appUser.cc);
        }
    
        @Override
        public int hashCode() {
            return Objects.hash(aa, bb, cc);
        }
    

  • 相关阅读:
    [TJOI2013]循环格
    [CQOI2017]小Q的表格
    【51nod 1514】 美妙的序列
    【bzoj3456】 城市规划
    [SHOI2010]最小生成树
    CF438E The Child and Binary Tree
    bzoj 4712: 洪水
    POJ-3069 Saruman's Army---区间选点
    POJ-3617 Best Cow Line---字符串贪心
    HDU-1850 Being a Good Boy in Spring Festival---尼姆博奕的运用
  • 原文地址:https://www.cnblogs.com/ruanjianlaowang/p/11182536.html
Copyright © 2011-2022 走看看