记录一个工具类,作用的将List集合里的map对象里面的value的值,去重!

package .warning.util;

import java.util.*;

/**
 * TODO:根据map里面两个字段去重
 * @author 
 * @version 1.0
 * @description TODO
 * @date 2019/10/18 18:08
 **/
public class ListMapUtil {

    public static List<Map<String, String>> toDistinctList(List<Map<String, String>> list) {

        //新建set集合,利用set集合元素不能重复特点,找出重复数据
        Set<DispatchCommandsPo> keysSet = new HashSet<DispatchCommandsPo>();

        //定义两个变量
        String equipmentNo = "";
        String depotName = "";

        Iterator<Map<String, String>> it=list.iterator();
        while(it.hasNext()) {
            Map<String, String> map=it.next();
            //变量用来保存两个key所对应的值
            equipmentNo = (String) map.get("equipmentNo");
            depotName = (String) map.get("depotName");

            //新建一个对象,并重写equals和hashcode方法
            DispatchCommandsPo  dispatchCommandsPo = new DispatchCommandsPo();
            dispatchCommandsPo.setEquipmentNo(equipmentNo);
            dispatchCommandsPo.setDepotName(depotName);

            int beforeSize = keysSet.size();
            keysSet.add(dispatchCommandsPo);
            int afterSize = keysSet.size();
            //判断当前对象是否保存进set集合中(若未保存说明set集合已存在该数据,删除该条map数据)
            if(afterSize != (beforeSize + 1)) {
                it.remove();
            }
        }
        return list;
    }
    static class DispatchCommandsPo {

        private String equipmentNo;
        private String depotName;

        public String getEquipmentNo() {
            return equipmentNo;
        }

        public void setEquipmentNo(String equipmentNo) {
            this.equipmentNo = equipmentNo;
        }

        public String getDepotName() {
            return depotName;
        }

        public void setDepotName(String depotName) {
            this.depotName = depotName;
        }

        //重写hashcode方法
        @Override
        public int hashCode() {
            int result = 17;
            result = 31 * result + (equipmentNo == null ? 0 : equipmentNo.hashCode());
            result = 31 * result + (depotName == null ? 0 : depotName.hashCode());
            return result;
        }

        //重写equals方法
        @Override
        public boolean equals(Object obj) {
            if (obj instanceof DispatchCommandsPo) {
                DispatchCommandsPo dispatchCommandsPo = (DispatchCommandsPo) obj;
                if (equipmentNo.equalsIgnoreCase(dispatchCommandsPo.getEquipmentNo().trim()) &&
                        depotName.equalsIgnoreCase(dispatchCommandsPo.getDepotName().trim())) {
                    return true;
                }
            }
            return false;
        }

    }


    public static List<Map<String, String>> toDistinctList2(List<Map<String, String>> list) {

        //新建set集合,利用set集合元素不能重复特点,找出重复数据
        Set<DispatchCommandsPo2> keysSet = new HashSet<DispatchCommandsPo2>();

        //定义两个变量
        String fenceName = "";

        Iterator<Map<String, String>> it=list.iterator();
        while(it.hasNext()) {
            Map<String, String> map=it.next();
            //变量用来保存两个key所对应的值
            fenceName = (String) map.get("fenceName");

            //新建一个对象,并重写equals和hashcode方法
            DispatchCommandsPo2  dispatchCommandsPo2 = new DispatchCommandsPo2();
            dispatchCommandsPo2.setFenceName(fenceName);


            int beforeSize = keysSet.size();
            keysSet.add(dispatchCommandsPo2);
            int afterSize = keysSet.size();
            //判断当前对象是否保存进set集合中(若未保存说明set集合已存在该数据,删除该条map数据)
            if(afterSize != (beforeSize + 1)) {
                it.remove();
            }
        }
        return list;
    }

    static class DispatchCommandsPo2 {

        private String fenceName;

        public String getFenceName() {
            return fenceName;
        }

        public void setFenceName(String fenceName) {
            this.fenceName = fenceName;
        }

        //重写hashcode方法
        @Override
        public int hashCode() {
            int result = 17;
            result = 31 * result + (fenceName == null ? 0 : fenceName.hashCode());
            return result;
        }

        //重写equals方法
        @Override
        public boolean equals(Object obj) {
            if (obj instanceof DispatchCommandsPo2) {
                DispatchCommandsPo2 dispatchCommandsPo2 = (DispatchCommandsPo2) obj;
                if (fenceName.equalsIgnoreCase(dispatchCommandsPo2.getFenceName().trim())) {
                    return true;
                }
            }
            return false;
        }

    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值