java集合合并相同元素

有一个List集合,带泛型的,List,Emp类有四个属性,id、name、orderCount和profit。现在需要把集合中id属性相同的元素合并成一个元素,就是像写sql一样,按id把数据分组,把orderCount和profit相加,orderCount和profit属性是int类型的,name属性不用管,因为每个id对应的name属性都不一样。怎么实现啊,最好用lamdba表达式

http://blog.csdn.net/doubeizhucele/article/details/43796303
你输出修改下就可以了。

package com.test;

import java.util.ArrayList;
import java.util.IntSummaryStatistics;
import java.util.List;
import java.util.stream.Collectors;

public class Test1 {

private class Emp{
    private String Id;
    private String name;
    private int orderCount;
    private int profit;
    /**
     * @return the id
     */
    public String getId() {
        return Id;
    }
    /**
     * @param id the id to set
     */
    public void setId(String id) {
        Id = id;
    }
    /**
     * @return the name
     */
    public String getName() {
        return name;
    }
    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }
    /**
     * @return the orderCount
     */
    public int getOrderCount() {
        return orderCount;
    }
    /**
     * @param orderCount the orderCount to set
     */
    public void setOrderCount(int orderCount) {
        this.orderCount = orderCount;
    }
    /**
     * @return the profit
     */
    public int getProfit() {
        return profit;
    }
    /**
     * @param profit the profit to set
     */
    public void setProfit(int profit) {
        this.profit = profit;
    }
    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return "Emp [Id=" + Id + ", name=" + name + ", orderCount=" + orderCount + ", profit=" + profit + "]";
    }


}

public static void main(String[] args) {
    Test1 t1 = new Test1();
    Emp emp = t1.new Emp();
    List<Emp> list = new ArrayList<Emp>();

    emp.setId("1");
    emp.setName("");
    emp.setOrderCount(1);
    emp.setProfit(2);

    list.add(emp);

    emp = t1.new Emp();
    emp.setId("1");
    emp.setName("");
    emp.setOrderCount(3);
    emp.setProfit(4);
    list.add(emp);

    emp = t1.new Emp();
    emp.setId("1");
    emp.setName("");
    emp.setOrderCount(5);
    emp.setProfit(6);
    list.add(emp);


    emp = t1.new Emp();
    emp.setId("2");
    emp.setName("");
    emp.setOrderCount(7);
    emp.setProfit(8);
    list.add(emp);

    list.stream().collect(Collectors.groupingBy(Emp::getId,Collectors.toList()))
                .forEach((id,empList)->{
                    IntSummaryStatistics orderCountSt = empList.stream()
                            .mapToInt(p -> p.getOrderCount()).summaryStatistics();
                    IntSummaryStatistics profitSt = empList.stream().mapToInt(q -> q.getProfit()).summaryStatistics();
                    String strFormat = String.format("id is %s, orderCount is %s, profit is %s", id, orderCountSt.getSum(), profitSt.getSum());
                    System.out.println(strFormat);
                });
}

}

用这个代码试试?