title: How to use an Interface as Map’s Key
body: <p>I am looking for help on the subject how to use an Interface as Maps Key. I tried to implement a solution, and get no compiletime errors but runtime errors when running my integration tests. Is it not possible to use an Interface as a Key, or is it my tests there is something wrong with?</p>
  
  <p>My code looks something like this</p>
  
  <pre><code>private Map&lt;AInterface, Values&gt; myMap = new HashMap&lt;AInterface, Values&gt;();
  </code></pre>
  
  <p>Upon retreiving the set of keys from myMap they do contain objects with expected Id, but are compared to not-equal. So when using myMap.get(Object key) i get null, eventhough an object with the same id is there. When using the concrete class instead of the interface all tests pass:</p>
  
  <pre><code>private Map&lt;AClass, Values&gt; myMap = new HashMap&lt;AClass, Values&gt;();
  </code></pre>
  
  <p>I've read <a href="https://docs.oracle.com/javase/8/docs/technotes/guides/language/generics.html" rel="nofollow noreferrer">Generics</a> where it states that for a Map, you are required to replace the type variables K and V by concrete types that are subtypes of Object. </p>
  
  <p>Since the compiler does not give me any warnings when using an Interface for K, my guess would have been that the tests have errors.</p>
  
  <p>Does anybody have any experience with using Interfaces as Key in a Map? And could give me any hints on what I am doing wrong?</p>