public class Test02 { public static void main(String[] args) { System.out.println(JSON.toJSONString(getSumPriceList())); } private static Map<String, Product> getSumPriceList () { List<Product> list = new ArrayList<>(); Product p1 = new Product("iphone", 9000); Product p2 = new Product("iphone", 8000); Product p3 = new Product("oppo", 6000); Product p4 = new Product("oppo", 7000); list.add(p1); list.add(p2); list.add(p3); list.add(p4); return list.stream().collect(Collectors.toMap(Product::getName, Function.identity(), (t1, t2) -> { t1.setPrice(t1.getPrice() + t2.getPrice()); return t1; })); } }
console
{"oppo":{"name":"oppo","price":13000},"iphone":{"name":"iphone","price":17000}}