package test2;
import java.util.*;
import java.util.stream.Collectors;
public class Fenzu {
public static void main(String[] args) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("name", "a");
map.put("price",500);
Map<String, Object> map2 = new HashMap<String, Object>();
map2.put("name", "a");
map2.put("price",1500);
Map<String, Object> map3 = new HashMap<String, Object>();
map3.put("name", "b");
map3.put("price",300);
List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
list.add(map);
list.add(map2);
list.add(map3);
List<Map<String,Object>> result =new ArrayList<>();
Map<String, List<Map<String, Object>>> glist = list.stream().collect(Collectors.groupingBy(e -> e.get("name").toString()));
glist.forEach((k,slist)->{
Map<String,Object> nmap=new HashMap<>();
IntSummaryStatistics sumcc = slist.stream().collect(Collectors.summarizingInt(e->Integer.valueOf(e.get("price").toString())));
nmap.put("name", slist.get(0).get("name"));
nmap.put("price", sumcc.getSum());//求和
result.add(nmap);
});
System.out.println("--------testMerge-------------");
result.forEach(x->{
System.out.println(x);
});
}
}
网友评论