List> 求和操作
List> salesList = this.baseMapper.sales(params);
int totalAaleCount = salesList.stream().mapToInt(m -> Integer.valueOf(String.valueOf(m.get("saleCount")))).reduce(0,Integer::sum);
//BigDecimal 求和
BigDecimal totalPrice = salesStandardList.stream().map(m -> new BigDecimal(m.get("totalAmount").toString())).reduce(BigDecimal.ZERO,BigDecimal::add);
List> 分组
public static void main(String[] args) {
List> mapList = new ArrayList<>();
Map map1 = new HashMap();
map1.put("name", "张三");
map1.put("age", 1);
map1.put("school", "北京大学");
Map map2 = new HashMap();
map2.put("name", "张三");
map2.put("age", 2);
map2.put("school", "清华大学");
Map map3 = new HashMap();
map3.put("name", "张三");
map3.put("age", 3);
map3.put("school", "青岛大学");
Map map4 = new HashMap();
map4.put("name", "李四");
map4.put("age", 9);
map4.put("school", "人民大学");
mapList.add(map1);
mapList.add(map2);
mapList.add(map3);
mapList.add(map4);
Map>> group = mapList.stream().collect(Collectors.groupingBy(m -> m.get("name")));
// System.out.println(group);
// {李四=[{school=人民大学, name=李四, age=9}], 张三=[{school=北京大学, name=张三, age=1}, {school=清华大学, name=张三, age=2}, {school=青岛大学, name=张三, age=3}]}
mapList = mapList.stream().collect(Collectors.groupingBy(e -> e.get("name")))
.entrySet().stream().map(e -> {
Map resultMap = new HashMap<>();
resultMap.put("items", e.getValue());
resultMap.put("name", e.getKey());
return resultMap;
}).collect(Collectors.toList());
System.out.println(mapList);
//[{name=李四, items=[{school=人民大学, name=李四, age=9}]}, {name=张三, items=[{school=北京大学, name=张三, age=1}, {school=清华大学, name=张三, age=2}, {school=青岛大学, name=张三, age=3}]}]
}
多个list合并分组
public static List> merge(List> m1, List> m2){
m1.addAll(m2);
Set set = new HashSet<>();
return m1.stream()
.collect(Collectors.groupingBy(o->{
//暂存所有key
set.addAll(o.keySet());
//按a_id分组
return o.get("a_id");
})).entrySet().stream().map(o->{
//合并
Map 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;
}).collect(Collectors.toList());
}
//多个List> 合并后然后根据key分组
public static void testListMap (){
Map map1 = new HashMap<>();
map1.put("itemName","iphone");
map1.put("price","1");
Map map2 = new HashMap<>();
map2.put("itemName","iphone");
map2.put("count","3");
Map map3 = new HashMap<>();
map3.put("itemName","huawei");
map3.put("price","1");
Map map4 = new HashMap<>();
map4.put("itemName","huawei");
map4.put("count","1");
List> list1 = new ArrayList<>();
list1.add(map1);
list1.add(map2);
List> list3 = new ArrayList<>();
list3.add(map3);
list3.add(map4);
list1.addAll(list3);
// System.out.println(ListUtil.merge(list1,"itemName"));
list1 = ListUtil.merge(list1,"itemName");
list1 = list1.stream().collect(Collectors.groupingBy(e -> e.get("itemName")))
.entrySet().stream().map(e -> {
Map mapa = new HashMap<>();
mapa.put("items", e.getValue());
mapa.put("itemName", e.getKey());
return mapa;
}).collect(Collectors.toList());
}
public static void main(String[] args) {
testListMap();
}