title: Set not applicable for Collection in generic method with Java
body: <p>I defined a generic function in java with the signature</p>
  
  <p><code>&lt;V&gt; List&lt;V&gt; sortedValuesFromMap(Map&lt;?, Collection&lt;V&gt;&gt; keysValues, Comparator&lt;V&gt; comp)</code></p>
  
  <p>which takes a Map mapping any type of keys to a Collection of some defined type V, and a comparator of type V. The method works great and the Java compiler does not complain about type incompatibility.</p>
  
  <p>But now when I want to apply this method to a map of the type <code>Map&lt;String, Set&lt;String&gt;&gt;</code> and the <code>AlphanumComparator</code> (see <a href="http://www.davekoelle.com/alphanum.html" rel="nofollow">here</a>) the compiler says :</p>
  
  <blockquote>
    <p>The method <code>sortedValuesFromMap(Map&lt;?,Collection&lt;V&gt;&gt;, Comparator&lt;V&gt;)</code>
    in the type MyUtils is not applicable for the arguments
    (<code>Map&lt;String,Set&lt;String&gt;, AlphanumComparator</code>)</p>
  </blockquote>
  
  <p>Turning <code>Collection</code> to <code>Set</code> in the signature of <code>sortedValuesFromMap</code> would fix it – but I do not want to do that. So why is Java forcing me to do so, although <code>Set&lt;E&gt;</code> is <a href="http://docs.oracle.com/javase/7/docs/api/java/util/Set.html" rel="nofollow">implementing</a> <code>Collection&lt;E&gt;</code>?</p>
  
  <p>PS: If someone is interested in my code:</p>
  
  <pre><code>public static &lt;V&gt; List&lt;V&gt; sortedValuesFromMap(Map&lt;?, Collection&lt;V&gt;&gt; keysValues,
          Comparator&lt;V&gt; comp) {
      List&lt;V&gt; values = new LinkedList&lt;V&gt;();
      for (Collection&lt;V&gt; col : keysValues.values()) {
          values.addAll(col);
      }
      Collections.sort(values, comp);
      return values;
  }
  </code></pre>