当前位置: 代码迷 >> 综合 >> jdk1.8 利用 stream 对List<Map<String, Object>> 分组 List<Map<String, Object>>排序
  详细解决方案

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

热度:25   发布时间:2024-02-09 08:24:25.0

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>>() {@Overridepublic 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)) ;}

 

  相关解决方案