对比两个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;
}
Java对于两个对象集合按照指定属性进行比较取出不同的元素,可以使用以下的方式: 1. 定义两个集合对象,分别存储两个对象集合。 2. 定义一个空的List集合,用于存储比较不同的元素。 3. 遍历第一个集合对象的每一个元素,然后通过指定属性的getter方法,获取指定属性。 4. 判断第二个集合对象是否存在与第一个元素相同的指定属性。如果不存在,则将该元素添加到List集合,表示该元素是第一个集合独有的元素。 5. 反之,则不做任何处理。 6. 遍历第二个集合对象的每一个元素,按照步骤3-5的方式进行比较,获取第二个集合独有的元素。 7. 最后将两个集合独有的元素合并成一个集合,就得到了按照指定属性比较两个对象集合不同的元素。 示例代码如下: ``` List<Object> list1 = ... // 第一个对象集合 List<Object> list2 = ... // 第二个对象集合 List<Object> diffList = new ArrayList<>(); // 存储不同元素的List集合 for (Object obj1 : list1) { String attr = obj1.getAttr(); // 获取指定属性 boolean exists = false; for (Object obj2 : list2) { if (attr.equals(obj2.getAttr())) { // 判断第二个集合是否存在相同的属性 exists = true; break; } } if (!exists) { // 如果不存在,则添加到List集合 diffList.add(obj1); } } for (Object obj2 : list2) { // 重复上面的操作,获取第二个集合独有的元素 String attr = obj2.getAttr(); boolean exists = false; for (Object obj1 : list1) { if (attr.equals(obj1.getAttr())) { exists = true; break; } } if (!exists) { diffList.add(obj2); } } // 打印不同的元素集合 System.out.println(diffList); ``` 这样就可以比较按照指定属性两个对象集合,并得到不同的元素集合了。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值