您的位置:首页 > 移动开发 > Objective-C

过滤掉List<HashMap<String,Object>>中键值相同的数据!

2016-10-24 10:08 519 查看
//初始化数据
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;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  hashmap 过滤
相关文章推荐