java8 List<Map<String, Object>>求和,排序,过滤,分组

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

Integer total = dataList.stream().mapToInt(e -> Integer.parseInt(e.get("num").toString())).sum(); //求num的总数量

dataList = dataList.stream().sorted((e1,e2) -> {

return -Double.compare(Double.valueOf(e1.get("num").toString()),Double.valueOf(e2.get("num").toString()));
}).collect(Collectors.toList()); //排序

dataList = dataList.stream().filter(e ->Integer.parseInt(e.get("caseNum").toString()) != 0).collect(Collectors.toList()); //过滤

Set<Map.Entry<String, List<Map<String, Object>>>> entries = dataList.stream()
.collect(Collectors.groupingBy(x -> x.get("code") + "_" + x.get("value") + "_" + x.get("name"))).entrySet(); //分组

 

public static void main(String[] args) {
List<Map<String, Object>> list = new ArrayList<>();
Map<String, Object> objectMap1 = new HashMap<>();
objectMap1.put("name","a");
objectMap1.put("num",10);
list.add(objectMap1);
Map<String, Object> objectMap2 = new HashMap<>();
objectMap2.put("name","b");
objectMap2.put("num",15);
list.add(objectMap2);
Map<String, Object> objectMap3 = new HashMap<>();
objectMap3.put("name","c");
objectMap3.put("num",22);
list.add(objectMap3);
int sum = list.stream().filter(x-> "a".equals(x.get("name").toString()) || "b".equals(x.get("name").toString())).mapToInt(a->((int) a.get("num"))).sum();
System.out.println(sum);
list.stream().filter(x-> "c".equals(x.get("name").toString())).map(a-> {a.put("num",sum); return a;}).collect(Collectors.toList());

}
 


posted @ 2021-10-19 17:36  zc1212  阅读(2197)  评论(0编辑  收藏  举报