title: java.util.Comparator.naturalOrder takes a <T extends Comparable<? super T>> and returns a Comparator<T> - why?
body: <p>(If this is a duplicate please point me to the right answer!  I searched and read several (>5) related questions but none seemed on the mark.  Also looked at the Generics FAQ and other sources...)</p>
  
  <p>It is apparently proper practice that when a collection class takes a comparator it should have the type <code>Comparator&lt;? super T&gt;</code> for your parameterized type <code>T</code>.  And you can see that lots of places, e.g., <a href="http://docs.oracle.com/javase/8/docs/api/java/util/TreeMap.html#TreeMap-java.util.Comparator-"><code>TreeMap</code></a>.  Okay.</p>
  
  <p>My problem is working with <a href="http://docs.oracle.com/javase/8/docs/api/java/util/Comparator.html#naturalOrder--"><code>Comparator.naturalOrder()</code></a> which is parameterized on <code>T extends Comparable&lt;? super T&gt;</code> but returns a <code>Comparator&lt;T&gt;</code>.  I'm trying to have a field in my collection class that holds either the user-specified comparator or the <code>Comparator.naturalOrder</code> comparator.</p>
  
  <p>I can't get it to work.  My questions, all related, are:</p>
  
  <ol>
  <li>How is <code>Comparator.naturalOrder</code> properly used?
  <ul>
  <li>And can I do what I want which is have a field where I store either a user-supplied comparator or the <code>naturalOrder</code> comparator?</li>
  </ul></li>
  <li>Given that most collection classes (in the framework) are parameterized on <code>T</code> not <code>T implements Comparable&lt;? super T&gt;</code>, so that's the design pattern chosen, how is <code>naturalOrder</code> useful since it requires the latter bounded wildcard, not the unconstrained type parameter?</li>
  </ol>
  
  <p>Thanks!</p>
  
  <p>Here follows the actual examples with compiler errors:</p>
  
  <p>So: If I have code like this in some class where T has no bounds (as in all existing collection classes):</p>
  
  <pre><code>class Foo&lt;T&gt; {
  
      private Comparator&lt;? super T&gt; comparator;
  
      public void someMethod(Comparator&lt;? super T&gt; comparator)
      {
          this.comparator = comparator;                  // no compile error
          this.comparator = Comparator.naturalOrder();   // incompatible types
      }
  }
  </code></pre>
  
  <p>with this error:</p>
  
  <pre><code>Error:(331, 50) java: incompatible types: inferred type does not conform to upper bound(s)
      inferred: T
      upper bound(s): java.lang.Comparable&lt;? super T&gt;
  </code></pre>
  
  <p>So if I decide to forgo the advantages of <code>? super T</code> then I have:</p>
  
  <pre><code>class Foo&lt;T&gt; {
  
      private Comparator&lt;T&gt; comparator;
  
      public void someMethod(ComparatorT&gt; comparator)
      {
          this.comparator = comparator;                  // no compile error
          this.comparator = Comparator.naturalOrder();   // incompatible types
      }
  }
  </code></pre>
  
  <p>where I have </p>
  
  <pre><code>Error:(nnn, 50) java: incompatible types: inference variable T has incompatible bounds
      equality constraints: T
      upper bounds: java.lang.Comparable&lt;? super T&gt;
  </code></pre>