美文网首页
List> 对Map中相

List> 对Map中相

作者: LX_代码制造坊 | 来源:发表于2020-02-17 13:10 被阅读0次
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);
        });
    }
}

相关文章

网友评论

      本文标题:List> 对Map中相

      本文链接:https://www.haomeiwen.com/subject/hisbfhtx.html