title: Count elements by type
body: <p>How can I map the elements in a list to be counted by their type using the Java Stream API?</p>
  
  <p>Without stream the example looks something like this:</p>
  
  <pre><code>    List&lt;Object&gt; list = new ArrayList&lt;&gt;();
      list.add("Hello");
      list.add(Integer.valueOf(1));
      list.add("World");
      list.add(Double.valueOf(4.6));
      list.add(Integer.valueOf(42));
      list.add(new Object());
      list.add(Integer.valueOf(4711));
      Map&lt;Class&lt;? extends Object&gt;, Integer&gt; map = new HashMap&lt;&gt;();
      for (Object item : list) {
          if (map.containsKey(item.getClass())) {
              int count = map.get(item.getClass());
              map.put(item.getClass(), count + 1);
          } else {
              map.put(item.getClass(), 1);
          }
      }
  </code></pre>
  
  <p>However in my production code it is not a list of <code>Object</code>, but of an interface with different implementation.</p>
  
  <p>My guess so far is something like this:</p>
  
  <pre><code>workshops.stream().collect(Collectors.groupingBy(Object::getClass))
  </code></pre>
  
  <p>But how can I map this to <code>Map&lt;Object, Integer&gt;</code> that will also work when <code>Object</code> is replaced by an interface (e.g. List)?</p>