在我们的印象中,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("/"); Mapmap = new HashMap (); 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 keySet = map.keySet(); Iterator 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> trfsVo = new HashMap > (); //判断是否重复 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 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 vo = new LinkedList (); vo.add(entity); trfsVo.put(key, vo); }