//初始化数据
List<HashMap<String,Object>> resultList = new ArrayList<HashMap<String,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>();
List<HashMap<String, Object>> result = new ArrayList<HashMap<String, 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<Map>
Set<String> mspKey = msp.keySet();
for(String key: mspKey){
HashMap newMap = msp.get(key);
newMap.put("empName", key);
result.add(newMap);
}
//返回最终的处理好的数据
return result;
过滤掉List<HashMap<String,Object>>中键值相同的数据!
最新推荐文章于 2024-08-07 18:48:15 发布