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?
My code looks something like this
private Map<AInterface, Values> myMap = new HashMap<AInterface, Values>();
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:
private Map<AClass, Values> myMap = new HashMap<AClass, Values>();
I've read http://java.sun.com/developer/technicalArticles/J2SE/generics/ 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.
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.
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?