重写java底层equals

来源:互联网 发布:2017支付宝和淘宝解绑 编辑:程序博客网 时间:2024/06/05 06:29
@Overridepublic boolean equals(Object obj) {    if (Objects.equals(obj.getClass().getName(), this.getClass().getName())) {        BuyerAttribute s = (BuyerAttribute) obj;        boolean same = Objects.equals(s.getBuyerNo(), this.getBuyerNo())                && Objects.equals(s.getAttribDefId(), this.getAttribDefId());        return same;    } else {        return super.equals(obj);    }}
/** * 当same为true时获取 serverList 和 clientList中都存在的数组 * 当same为false时 获取在clientList中存在而在serverList中不存在的数组 * 使用这个方法需要重写T的equals方法,具体判断逻辑视具体的情况而定 * * @param same * @param serverList * @param clientList * @param <T> * @return */public static <T> List<T> getSameOrDiffList(List<T> serverList, List<T> clientList, boolean same) {    if (ListUtil.isBlank(serverList) || ListUtil.isBlank(clientList)) {        return clientList;    }    List<T> result = new ArrayList<>();    for (T t : clientList) {        if (same && serverList.contains(t)) {            result.add(t);        } else if (!same && !serverList.contains(t)) {            result.add(t);        }    }    return result;}

原创粉丝点击