It seems to me that any scenario where I instantiate a HashMap
using an interface for a key-type would work contrary to the idea of the map. Take the following example:
HashMap<MyInterface, Integer> map = new HashMap<MyInterface, Integer>();
If I put 2 objects of different MyInterface
implementations into the map then they will both operate on their own hashCode()
implementations and the entire underlying logic of the HashMap
no longer makes sense. So my question is, is there ever a proper time for using interfaces as keys in HashMaps
?