HashMap 分拣存储 实现一对多映射

在我们的印象中,Map k,v 映射,一对一的比较多,下面主要讲一对多的关系映射,主要需求在于,需要把很多的,杂乱的数据 按照不同的类型进行分类处理,A,B,C类型的数据进行分类统计
比如,计算字母/单词出现的次数
String str = "t/h/is/is/a/car/a/n/d/t/ha/t/is/tr/uck/a/nd/w/h/e/r/e/is/t/he/s/t/uffs";

String [] str =
              "t/h/is/is/a/car/a/n/d/t/ha/t/is/tr/uck/a/nd/w/h/e/r/e/is/t/he/s/t/uffs".split("/");
      Map<String,Integer> map 
              = new HashMap<String,Integer>();
      for(String key:str){
          //方法1
          //检查map里面是否存在这个单词,如果不存在 说明之出现一次
//          if(!map.containsKey(key)){
//              map.put(key, 1);
//          }else{
//              map.put(key, map.get(key)+1);//存在 就在前面的基础上面+1 计算出现的次数
//          }
          //方法2
          Integer value = map.get(key);
          if(value == null){
              map.put(key, 1);
          }else{
              map.put(key, value+1);
          }
      }
      Set<String> keySet = map.keySet();
      Iterator<String>it = keySet.iterator();
      while(it.hasNext()){
          String key = it.next();
          Integer value = map.get(key);
          System.out.println(key+"-->"+value);
      }
    }

当然,这里的value也可以是对象,对于不同的场景 进行灵活的应用。

实际需求中如何运用HashMap实现过滤重复
这里的重复不一定是某个字段一样的,而且时间点不重叠 这里需要校验不重叠的字段 有 "员工所在部门","出发点","目的点","生效日期","失效日期","出发时点","到达时点","出发分钟","到达分钟"

entity 是需要校验的对象。

   final Map<String, List<TransferNumUnitPrice>> trfsVo = new HashMap<String, List<TransferNumUnitPrice>> ();
   //判断是否重复
            if (entity.getDeptCode()!=null && entity.getBeginDeptCode() != null &&
                entity.getEndDeptCode()!=null && entity.getEffectiveDate() != null && entity.getInvalidDate() != null ) {
                //三个点重复就继续判断时间点是否重叠
                String key = StringUtil.append(entity.getDeptCode(),'_',entity.getBeginDeptCode(),'_',entity.getEndDeptCode());
                if (trfsVo.containsKey(key)) {
                    List<TransferNumUnitPrice> vo = trfsVo.get(key);
                    Calendar now = Calendar.getInstance();
                    now.set(Calendar.HOUR_OF_DAY, entity.getBeginTimePoint());
                    now.set(Calendar.MINUTE, entity.getBeginTimeMinute());
                    long entity_start = now.getTime().getTime();
                    now.set(Calendar.HOUR_OF_DAY, entity.getEndTimePoint());
                    now.set(Calendar.MINUTE, entity.getEndTimeMinute());
                    long entity_end = now.getTime().getTime();
                    for(TransferNumUnitPrice vor:vo) {
                        
                        //entity代表原来excel里面的数据,vor 从excel里面过滤处理出来有3个code重复的数据
                        if (    //过滤生效日期在区间范围重叠
                                ((
                                (vor.getEffectiveDate().compareTo(entity.getEffectiveDate())!=1)
                                &&
                                (vor.getInvalidDate().compareTo(entity.getEffectiveDate())!=-1)
                                )
                                ||
                                //过滤失效日期在区间范围重叠
                                (
                                (vor.getEffectiveDate().compareTo(entity.getInvalidDate())!=1)
                                &&
                                (vor.getInvalidDate().compareTo(entity.getInvalidDate())!=-1)
                                )
                                ||
                                //过滤包含重叠
                                (
                                (vor.getEffectiveDate().compareTo(entity.getEffectiveDate())==1)
                                &&
                                (vor.getInvalidDate().compareTo(entity.getInvalidDate())==-1)
                                ))
                        ) {
                            now.set(Calendar.HOUR_OF_DAY, vor.getBeginTimePoint());
                            now.set(Calendar.MINUTE, vor.getBeginTimeMinute());
                            long vorhm_start = now.getTime().getTime();
                            now.set(Calendar.HOUR_OF_DAY, vor.getEndTimePoint());
                            now.set(Calendar.MINUTE, vor.getEndTimeMinute());
                            long vorhm_end = now.getTime().getTime();
                            if (  //开始时间在区间范围中
                                    (vorhm_start <= entity_start
                                    &&
                                    vorhm_end >= entity_start)
                                    ||
                                  //结束时间在区间范围中
                                    (vorhm_start <= entity_end
                                    &&
                                    vorhm_end >= entity_end)
                                    ||
                                  // 时间包含 
                                    (vorhm_start > entity_start
                                    &&
                                    vorhm_end < entity_end)
                                    ) {
                                //加入异常 比较异常
                                vo.add(entity);
                                throw new NovatarRuntimeException(getMessageSource().getMessage("transferNumUnitPrice.excelDataConflict"));
                            }
                        }
                    }
                    //加入需要比较的无异常的数据 进行比较
                    vo.add(entity);
                } else {
                    List<TransferNumUnitPrice> vo = new LinkedList<TransferNumUnitPrice>();
                    vo.add(entity);
                    trfsVo.put(key, vo);
                }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值