对比两个list集合中对象不同的属性,并取出不同的地方

@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class DifferenceWapper implements Serializable {
    private static final long serialVersionUID = -3369182406683473741L;
    private String column;
    private Difference difference;
    private String cloumnName;
}
@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Difference  {
    private static final long serialVersionUID = 2478199484126795290L;

    private  Object oldValue; //对应的旧值
    private String oldChangeValue; //旧值转换之后的值
    private Object newValue; //对应的新值
    private String newChangeValue; //新值转变之后的值
}
public class PosInfoVo {
   // pos型号
   private String trmMod;
   // pos数量(台)
   private Integer trmQuantity;
   public PosInfoVo() {

   }

   public PosInfoVo(String trmMod, Integer trmQuantity) {
      this.trmMod = trmMod;
      this.trmQuantity = trmQuantity;
   }
   public void setTrmMod(String trmMod) {
      this.trmMod = trmMod;
   }

   /**
    * pos型号
    * 
    * @return
    */
   public String getTrmMod() {
      return trmMod;
   }

   public void setTrmQuantity(Integer trmQuantity) {
      this.trmQuantity = trmQuantity;
   }

   public Integer getTrmQuantity() {
      return trmQuantity;
   }

  
}
public List<DifferenceWapper> comparePosInfoVo(List<PosInfoVo> first,List<PosInfoVo> second, List<DifferenceWapper> list){
    HashMap<String, Integer> firstHashMap = new HashMap();
    if (null!=first &&!first.isEmpty()){
        for (PosInfoVo posInfoVo : first) {
            if (posInfoVo.getTrmMod() != null) {
                posInfoVo.setTrmQuantity(posInfoVo.getTrmQuantity() == null ? 0 : posInfoVo.getTrmQuantity());
                if (null != firstHashMap.get(posInfoVo.getTrmMod())) {
                    firstHashMap.put(posInfoVo.getTrmMod(),
                            firstHashMap.get(posInfoVo.getTrmMod()) + posInfoVo.getTrmQuantity());
                } else {
                    firstHashMap.put(posInfoVo.getTrmMod(), posInfoVo.getTrmQuantity());
                }
            }
        }
    }
    HashMap<String, Integer> secondHashMap = new HashMap();

    if (null!=second && !second.isEmpty()){
        for (PosInfoVo posInfoVo : second) {
            if (posInfoVo.getTrmMod() != null) {
                posInfoVo.setTrmQuantity(posInfoVo.getTrmQuantity() == null ? 0 : posInfoVo.getTrmQuantity());
                if (null != secondHashMap.get(posInfoVo.getTrmMod())) {
                    secondHashMap.put(posInfoVo.getTrmMod(),
                            secondHashMap.get(posInfoVo.getTrmMod()) + posInfoVo.getTrmQuantity());
                } else {
                    secondHashMap.put(posInfoVo.getTrmMod(), posInfoVo.getTrmQuantity());
                }
            }
        }
    }


    HashSet hashSet = new HashSet();
    hashSet.addAll(firstHashMap.keySet());
    hashSet.addAll(secondHashMap.keySet());
    Iterator<String> it = hashSet.iterator();
    while (it.hasNext()) {
        DifferenceWapper differenceWapper = new DifferenceWapper();
        String next = it.next();
        int secondFi = secondHashMap.get(next) == null ? 0 : secondHashMap.get(next);
        int firstFi = firstHashMap.get(next) == null ? 0 : firstHashMap.get(next);
        if (secondFi != firstFi) {
            differenceWapper.setColumn(next);
            differenceWapper.setCloumnName(next);
            Difference difference = new Difference();
            difference.setOldChangeValue(Integer.valueOf(firstFi).toString());
            difference.setOldValue(Integer.valueOf(firstFi).toString());
            difference.setNewValue(Integer.valueOf(secondFi).toString());
            difference.setNewChangeValue(Integer.valueOf(secondFi).toString());
            differenceWapper.setDifference(difference);
            list.add(differenceWapper);
        }

    }
    return list;
}

猜你喜欢

转载自blog.csdn.net/m0_37708405/article/details/84900124