过滤掉List中键值相同的数据!

//初始化数据
ListString,Object>> resultList = new ArrayListString,Object>>();
HashMap<String,Object> result1 = new HashMap<String,Object>();
//添加数据result1
result.put("colour","红色");
result.put("empName", "张三");
result.put("depotType", "常驻");
resultList.add(result1);
//添加数据result2 
HashMap<String,Object> result2 = new HashMap<String,Object>();
result.put("colour","红色");
result.put("empName", "张三");
result.put("depotType", "常驻");
resultList.add(result2);
//添加数据result3 
HashMap<String,Object> result3 = new HashMap<String,Object>();
result.put("colour","蓝色");
result.put("empName", "张三");
result.put("depotType", "外包");
resultList.add(result3);
HashMap<String, HashMap> msp = new HashMap<String, HashMap>();
ListString, Object>> result = new ArrayListString, Object>>();
//把list中的数据转换成msp,去掉同一empName值多余数据,保留查找到第一个empName值对应的数据
for(int i = resultList.size()-1; i>=0; i--){
    HashMap map = resultList.get(i);
       String name = (String)map.get("empName");
       map.remove("empName");
       msp.put(name, map);
   }
//把msp再转换成list,就会得到根据某一字段去掉重复的数据的List
Set<String> mspKey = msp.keySet();
for(String key: mspKey){
    HashMap newMap = msp.get(key);
    newMap.put("empName", key);
    result.add(newMap);
}
//返回最终的处理好的数据
return  result;

你可能感兴趣的:(java)