Map 和 Map 比较

2020-08-03 23:17:29 浏览数 (1)

String,String

代码语言:javascript复制
/**
     * 用map的keySet()的迭代器(性能效率较低)
     * 
     */
    public  void compareMap1 (){
 
        Map<String, String> m1 = new HashMap<String, String>();//小
        Map<String, String> m2 = new HashMap<String, String>();//大
 
        Iterator<String> iter1 = m1.keySet().iterator();
 
        while (iter1.hasNext()) {
            String m1Key = (String) iter1.next();
            if (!m1.get(m1Key).equals(m2.get(m1Key))) {//若两个map中相同key对应的value不相等
                //......
            }
        }
 
    }
    /**
     * 用map的entrySet()的迭代器(性能效率较高)
     */
    public void compareMap2(){
        Map<String, String> m1 = new HashMap<String, String>();
        Map<String, String> m2 = new HashMap<String, String>();
         
        Iterator<Entry<String, String>> iter1 = m1.entrySet().iterator();
        while(iter1.hasNext()){
            Map.Entry<String, String> entry1 = (Entry<String, String>) iter1.next();
       String m1value = entry1.getValue() == null?"":entry1.getValue();
            String m2value = m2.get(entry1.getKey())==null?"":m2.get(entry1.getKey());
                  
            if (!m1value.equals(m2value)) {//若两个map中相同key对应的value不相等
                //其他操作...
            }
        }
    }
    /**
     * 用map的entrySet()的增强型for循环(性能效率较高)
     */
    public void compareMap3(){
         Map<String, String> m1 = new HashMap<String, String>();
         Map<String, String> m2 = new HashMap<String, String>();
          
         for(Map.Entry<String, String> entry1:m1.entrySet()){
             String m1value = entry1.getValue() == null?"":entry1.getValue();
             String m2value = m2.get(entry1.getKey())==null?"":m2.get(entry1.getKey());
             if (!m1value.equals(m2value)) {//若两个map中相同key对应的value不相等
                //其他操作...
            }
         } 
    }

String,List

项目中运用到,记录一下

代码语言:javascript复制
// 车牌缓存 - 进入离开
Map<String, List<VehicleAnomalyVO>> inNumberMap = Maps.newHashMap();
    Map<String, List<VehicleAnomalyVO>> outNumberMap = Maps.newHashMap();

// 用于存放车辆进入工地信息
   List<VehicleAnomalyVO> inList = Lists.newArrayList();
   // 用于存放车辆离开工地信息
   List<VehicleAnomalyVO> outList = Lists.newArrayList();

// 循环检查,是否已存在key,如果存在则直接添加到key的list中,否则新增key和新增key对应的list
for (VehicleAnomalyVO in :
		inList) {
	if (numberMap.get(in.getRPlateNumber()) == null) {
		List<VehicleAnomalyVO> list = Lists.newArrayList();
		list.add(in);
		numberMap.put(in.getRPlateNumber(), list);
	} else {
		numberMap.get(in.getRPlateNumber()).add(in);
	}
}

for (VehicleAnomalyVO out :
		outList) {
	out.setState("4");
	if (outNumberMap.get(out.getRPlateNumber()) == null) {
		List<VehicleAnomalyVO> list = Lists.newArrayList();
		list.add(out);
		outNumberMap.put(out.getRPlateNumber(), list);
	} else {
		outNumberMap.get(out.getRPlateNumber()).add(out);
	}
}

// 开始比较
for (Map.Entry<String, List<VehicleAnomalyVO>> entry :
	  inNumberMap.entrySet()) {

	List<VehicleAnomalyVO> mapInList = entry.getValue();
	List<VehicleAnomalyVO> mapOutList = outNumberMap.get(entry.getKey());

	//System.out.println("**************************");
	//System.out.println("mapInList = "   mapInList);
	//System.out.println("mapOutList = "   mapOutList);
	//System.out.println("**************************");


	VehicleAnomalyVO vo;

	for (int i = 0; i < mapInList.size(); i  ) {
		vo = new VehicleAnomalyVO();
		vo.setHist(new ZtPositionHist(mapInList.get(0).getRPlateNumber()));
		vo.setSynchroDate(getLatestSynDate());
		vo.setReceiveDateStart(mapInList.get(i).getReceiveDate());
		if (mapOutList != null && i < mapOutList.size()) {
			vo.setReceiveDateEnd(mapOutList.get(i).getReceiveDate());
			vo.setState("正常进入离开时间");
		} else {
			vo.setState("缺少离开时间");
		}
	}
}

0 人点赞