title: Java 8 Stream - GroupingBy Class
body: <p>i have a problem with groupingBy method.
  I use an example:
  Suppose to have a <code>List&lt;Person&gt;</code> where <code>Person.class</code> and <code>City.class</code> are like this</p>
  
  <pre><code>class Person{
     String name;
     String surname;
     City city;
  }
  
  class City{
     String name;
     String postalCode;
  }
  </code></pre>
  
  <p>I would group the list by <code>city.postalcode</code> and do somthing like this:</p>
  
  <pre><code> List&lt;Person&gt; myList = ...some persons here...
   Map&lt;City,List&lt;Person&gt;&gt; groupMap = myList.stream().collect(
              Collectors.groupingBy(person -&gt; person));
  </code></pre>
  
  <p>Obviously this not work because <code>groupingBy</code> don't know how to group by an object.
  I would do something like this <code>Collectors.groupingBy(person -&gt; person.city.postalcode)</code> but with this produces a map of type <code>Map&lt;String,List&lt;Person&gt;&gt;</code> and not  <code>Map&lt;City,List&lt;Person&gt;&gt;</code></p>
  
  <p>Thanks for any help.</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>For use <code>groupingBy</code> on a class this must simply implement correctly  <code>equals</code> and <code>hashcode</code></p>