java8 List>求和,排序,过滤,分组


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());

}
 



免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM