title: Java collection - group by multiple attributes
body: <p>I have a Java class:</p>
  
  <pre><code>class Person {
    String firstName;
    String lastName;
    int income;
  
  public Person(String firstName, String lastName, int income)
  {
    this.firstName = firstName;
    this.lastName = lastName;
    this.income = income;
  }
  </code></pre>
  
  <p>I have a <code>Collection&lt;Person&gt;</code>, with 4 x Person objects:</p>
  
  <pre><code>  Collection&lt;Person&gt; persons = new ArrayList&lt;&gt;();
    persons.add(new Person("John", "Smith", 5));
    persons.add(new Person("Mary", "Miller", 2));
    persons.add(new Person("John", "Smith", 4));
    persons.add(new Person("John", "Wilson", 4));
  </code></pre>
  
  <p>I want to make a new Collection instance, but from the elements with the same "firstName" and "lastName", make 1 element, and the result "income" will be the sum of the "incomes" of each element. So, for this particular case, the resulting collection will have 3 elements, and "John Smith" will have the "income" = 9. </p>
  
  <p>In SQL, the equivalent query is:</p>
  
  <pre><code>SELECT FIRSTNAME, LASTNAME, SUM(INCOME) FROM PERSON GROUP BY FIRSTNAME, LASTNAME
  </code></pre>
  
  <p>I found only answers which contain Map as result, and "key" contains the column(s) used for grouping by. I want to obtain directly a similar type of collection from the initial (<code>ArrayList&lt;Person&gt;</code>), and not a Map, because if in my collection I have millions of elements, it will decrease the performance of the code. I know it was easier if I worked on SQL side, but in this case I must work on Java side.</p>