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

//初始化数据
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;
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值