listmap根据某字段进行合并
public List<Map<String, Object>> mergeResult(List<Map<String, Object>> m1, List<Map<String, Object>> m2, String key) { m1.addAll(m2); Set<String> set = new HashSet<>(); List<Map<String, Object>> resultList = m1.stream().collect(Collectors.groupingBy(o -> { // 暂存所有key set.addAll(o.keySet()); return o.get(key); })).entrySet().stream().map(o -> { // 合并 Map<String, Object> map = o.getValue().stream().flatMap(m -> { return m.entrySet().stream(); }).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (a, b) -> b)); // 为没有key的赋值0 set.stream().forEach(k -> { // if (!map.containsKey(k)) // map.put(k, 0); }); return map; }).sorted((map1,map2)->{ return map1.get(key).toString().compareTo(map2.get(key).toString()); }).collect(Collectors.toList()); return resultList; }
昔日我曾苍老,如今风华正茂(ง •̀_•́)ง