title: Why are these types not assignment compatible, and how can I define an assignment compatible class?
body: <p>I implement a map based on two type arguments <code>&lt;K,V&gt;</code>, where <code>K</code> is the key type and <code>V</code> is the value type.</p>
  
  <pre><code>public class Map&lt;K,V&gt; implements Map&lt;K,V&gt;
  { .. implementation .. }
  </code></pre>
  
  <p>One method in the <code>Map</code> interfaces returns the set of map entries.</p>
  
  <pre><code>public Set&lt;java.util.Map.Entry&lt;K,V&gt;&gt; entrySet()
  </code></pre>
  
  <p>Since my map implementation is based on an AVL tree, another class must be implemented as <code>Set&lt;MapNode&gt;</code></p>
  
  <pre><code>public class EntrySet&lt;K,V&gt; implements Set&lt;MapNode&lt;K,V&gt;&gt;
  { .. implementation ..}
  </code></pre>
  
  <p>In order to tell that the nodes of the map tree are indeed map entries, they are defined as follows:</p>
  
  <pre><code>public class MapNode&lt;K,V&gt; implements Map.Entry&lt;K,V&gt;
  { .. implementation .. }
  </code></pre>
  
  <p>So EntrySet contains the same tree as the <code>Map</code> itself, but treats the tree nodes as the set elements (which is why it has to be implemented separately).</p>
  
  <p>The method in the map implementation, returning the set of entries, should therefore look as follows:</p>
  
  <pre><code>/* (non-Javadoc)
   * @see java.util.Map#entrySet()
   */
  @Override
  public Set&lt;java.util.Map.Entry&lt;K,V&gt;&gt; entrySet()
  {
      return new EntrySet&lt;K,V&gt;(this.comparator,this.tree);
  }
  </code></pre>
  
  <p>Yet, the compiler gives me the following error:</p>
  
  <p>"Type mismatch: cannot convert from <code>EntrySet&lt;K,V&gt;</code> to <code>Set&lt;Map.Entry&lt;K,V&gt;&gt;</code>"</p>
  
  <p><strong>My questions:</strong></p>
  
  <ul>
  <li>Is my following assumption correct? (Or if not, what is the problem?)</li>
  </ul>
  
  <p>A set of <code>Map.Entry&lt;K,V&gt;</code> would have to be able to contain <em>all</em> sorts of <code>Map.Entry&lt;K,V&gt;</code>, not just the <code>MapNode&lt;K,V&gt;</code> objects. It is therefore, logically, not the case that <code>EntrySet&lt;K,V&gt;</code> <em>is</em> a <code>Set&lt;Map.Entry&lt;K,V&gt;</code>, and therefore these two set types are not considered assignment compatible.</p>
  
  <ul>
  <li>Is there any other way to define a class <code>EntrySet&lt;K,V&gt;</code>, based on the same tree structure (I do <em>not</em> want to increase run time by creating or invoking another data structure) such that an object of this class can be returned wherever <code>Set&lt;Map.Entry&lt;K,V&gt;</code> is required?</li>
  </ul>
  
  <p><strong>Update:</strong></p>
  
  <p>I have tried the following version:</p>
  
  <pre><code> public class EntrySet&lt;K,V&gt; implements Set&lt;MapNode&lt;K,V&gt;&gt;
   { .. implementation ..}
  </code></pre>
  
  <p>In this case the iterator creates a similar problem. There exists an iterator class that neatly traverses the whole tree, but it is defined as the iterator of a super class of <code>MapNode&lt;K,V&gt;</code>, and this iterator cannot be cast to the required <code>Iterator&lt;Map.Entry&lt;K,V&gt;&gt;</code></p>
  
  <pre><code>@Override
  public Iterator&lt;Map.Entry&lt;K,V&gt;&gt; iterator()
  {
      // this iterator type can not be cast to the desired return type
      return new TreeNodeIterator&lt;MapNode&lt;K,V&gt;,K&gt;(this.tree);
  }
  </code></pre>
  
  <p>I might copy the whole iterator code and rudely use it in a new, independent iterator class, but I'd prefer a more elegant solution.</p>
  
  <p>So I am still stuck here.</p>
  
  <p><strong>Update:</strong></p>
  
  <p>In the mean time, I have created another independent iterator class that boxes the original iterator and converts the "next" element accordingly. This is not really elegant, but at least it works for the time being. Still, any more elegant solution will be very welcome!</p>