jdk1.8 利用 stream 对List<Map<String, Object>> 分组 List<Map<String, Object>>排序

  • Post author:
  • Post category:其他




亲测可用,若有疑问请私信

检索关键字List<Map<String, Object>> 分组

检索关键字:jdk 8 list<map<String,Object>> 排序

//根据工号进行分组

List<Map<String, Object>> dataList = ((PageResult<Map<String, Object>>) tuple.e2).getItems();

Map<String, List<Map<String, Object>>> groupByEmpId= dataList.stream().collect(

groupingBy(map -> map.get(“emp_id”).toString()));

List<Map<String, Object>>排序

//排序获取id最大的数据

List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();

for(Map.Entry<String, List<Map<String, Object>>> entry : groupByEmpId.entrySet()){


List<Map<String, Object>> mapValue = entry.getValue();

Collections.sort(mapValue, new Comparator<Map<String, Object>>() {


@Override

public int compare(Map<String, Object> o1, Map<String, Object> o2) {


//此处o2.get(“id”)返回BigInteger类型转Integer需要先转String中转

return Integer.valueOf(o2.get(“id”).toString()) – Integer.valueOf(o1.get(“id”).toString());

}

});

result.add(CollectionUtils.isEmpty(mapValue.get(0)) ? null : mapValue.get(0)) ;

}