两个对象list 标识出 对象中相同的 与不同的

private Map<Integer, List<QueryAllLabelsResponse>> findListDiff(List<QueryAllLabelsResponse> rps1, List<QueryAllLabelsResponse> rps2){
    //判断不能为空
    if(rps1 == null || rps1.isEmpty() || rps2 == null || rps1.isEmpty()) return null;
    //保存最后的数据
    Map<Integer, List<QueryAllLabelsResponse>>  mapList = new HashMap<Integer, List<QueryAllLabelsResponse>>(3);

    //复制rps1,作为备份
    List<QueryAllLabelsResponse> rps1_bak = new ArrayList<QueryAllLabelsResponse>(rps1);

    //1、获取rps1中与rps2中不同的元素
    rps1.removeAll(rps2);

    //2、获取rps1和rps2中相同的元素
    rps1_bak.removeAll(rps1);

    //3、获取rps2中与rps1中不同的元素
    rps2.removeAll(rps1_bak);

    //经过此转换后rps1中数据与rps2中的数据完全不同
    //rps1_bak是rps1和rps2的交集
    //rps2中的数据与rps1中的数据完全不同

    mapList.put(0, rps1);//rps1中独有的数据
    mapList.put(1, rps1_bak);//交集的数据
    mapList.put(2, rps2);//rps2中的独有数据


    return mapList;
}


QueryAllLabelsResponse  对象需要重写hashCode与equals  

@Override
public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;
    QueryAllLabelsResponse that = (QueryAllLabelsResponse) o;
    return Objects.equals(labelId, that.labelId) &&
            Objects.equals(parentId, that.parentId) &&
            Objects.equals(labelType, that.labelType);
}
只要 这三个参数一样 就认为两个对象为相同的    


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值