Aggregate List of objects in Java
<p>Do we have any aggregator function in Java to perform the below aggregation?</p>

<pre><code>Person {
    String name;
    String subject;
    String department;
    Long mark1;
    Long mark2;
    Long mark3;
}
</code></pre>

<p>List contains data as below.</p>

<pre>
Name    |Subject    |Department |Mark1  |Mark2  |Mark3
--------|-----------|-----------|-------|-------|-----
Clark   |English    |DEP1       |7      |8      |6
Michel  |English    |DEP1       |6      |4      |7
Dave    |Maths      |DEP2       |3      |5      |6
Mario   |Maths      |DEP1       |9      |7      |8
</pre>

<p>The aggregation criteria is Subject &amp; Dep. The resultant object needs to be</p>

<pre>
Subject     |Department |Mark1  |Mark2  |Mark3
----------- |-----------|-------|-------|-----
English     |DEP1       |13     |12     |13
Maths       |DEP2       |3      |5      |6
Maths       |DEP1       |9      |7      |8
</pre>

<p>This aggregation can be achieved by manually iterating through the list and create an aggregated list. Example as below.</p>

<pre><code>private static List&lt;Person&gt; getGrouped(List&lt;Person&gt; origList) {
    Map&lt;String, Person&gt; grpMap = new HashMap&lt;String, Person&gt;();

    for (Person person : origList) {
        String key = person.getDepartment() + person.getSubject();
        if (grpMap.containsKey(key)) {
            Person grpdPerson = grpMap.get(key);
            grpdPerson.setMark1(grpdPerson.getMark1() + person.getMark1());
            grpdPerson.setMark2(grpdPerson.getMark2() + person.getMark2());
            grpdPerson.setMark3(grpdPerson.getMark3() + person.getMark3());
        } else {
            grpMap.put(key, person);
        }
    }
    return new ArrayList&lt;Person&gt;(grpMap.values());
}
</code></pre>

<p>But is there any aggregation function or feature of Java 8 which we can leverage?</p>