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

来源:互联网 发布:安畅网络水军平台 编辑:程序博客网 时间:2024/04/30 08:10
//初始化数据List<HashMap<String,Object>> resultList = new ArrayList<HashMap<String,Object>>();HashMap<String,Object> result1 = new HashMap<String,Object>();//添加数据result1result.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;
0 0
原创粉丝点击