title: Group by multiple field names in java 8
body: <p>I found the code for grouping the objects by some field name from POJO. Below is the code for that:</p>
  
  <pre><code>public class Temp {
  
      static class Person {
  
          private String name;
          private int age;
          private long salary;
  
          Person(String name, int age, long salary) {
  
              this.name = name;
              this.age = age;
              this.salary = salary;
          }
  
          @Override
          public String toString() {
              return String.format("Person{name='%s', age=%d, salary=%d}", name, age, salary);
          }
      }
  
      public static void main(String[] args) {
          Stream&lt;Person&gt; people = Stream.of(new Person("Paul", 24, 20000),
                  new Person("Mark", 30, 30000),
                  new Person("Will", 28, 28000),
                  new Person("William", 28, 28000));
          Map&lt;Integer, List&lt;Person&gt;&gt; peopleByAge;
          peopleByAge = people
                  .collect(Collectors.groupingBy(p -&gt; p.age, Collectors.mapping((Person p) -&gt; p, toList())));
          System.out.println(peopleByAge);
      }
  }
  </code></pre>
  
  <p>And the output is (which is correct):</p>
  
  <pre><code>{24=[Person{name='Paul', age=24, salary=20000}], 28=[Person{name='Will', age=28, salary=28000}, Person{name='William', age=28, salary=28000}], 30=[Person{name='Mark', age=30, salary=30000}]}
  </code></pre>
  
  <p>But what if I want to group by multiple fields? I can obviously pass some POJO in <code>groupingBy()</code> method after implementing <code>equals()</code> method in that POJO but is there any other option like I can group by more than one fields from the given POJO?</p>
  
  <p>E.g. here in my case, I want to group by name and age.</p>