title: How to write a mode method using streams
body: <p>I am trying to write a method to get the mode of a <code>Collection</code> using a <code>Comparator</code>. </p>
  
  <p>Please can somebody show me what changes I have to make to get this to compile? I do not want to change the signature.</p>
  
  <pre><code>static &lt;T&gt; T mode(Collection&lt;? extends T&gt; collection, Comparator&lt;? super T&gt; comparator) {
      return collection.stream()
                       .collect(Collectors.groupingBy(t -&gt; t, () -&gt; new TreeMap&lt;&gt;(comparator), Collectors.counting()))
                       .entrySet()
                       .stream()
                       .reduce(BinaryOperator.maxBy(Comparator.comparingLong(Map.Entry::getValue)))
                       .map(Map.Entry::getKey)
                       .orElseThrow(IllegalArgumentException::new);
  }
  </code></pre>
  
  <p><strong>EDIT</strong></p>
  
  <p>It turns out I was just using the wrong version. This does not compile using <code>javac 1.8.0_25</code>. The exact three error messages are: </p>
  
  <pre><code>Error:(40, 47) java: incompatible types: inferred type does not conform to upper bound(s)
  inferred: java.lang.Object
  upper bound(s): T,java.lang.Object
  
  Error:(43, 45) java: incompatible types: cannot infer type-variable(s) T
  (argument mismatch; invalid method reference
    method getValue in interface java.util.Map.Entry&lt;K,V&gt; cannot be applied to given types
      required: no arguments
      found: java.lang.Object
      reason: actual and formal argument lists differ in length)
  
  Error:(44, 25) java: invalid method reference
  non-static method getKey() cannot be referenced from a static context
  </code></pre>
  
  <p>However, I have since upgraded to <code>javac 1.8.0_65</code> and it compiles perfectly.</p>