A coworker encountered a bug where HashMap#containsKey was used to verify if a key-value pair exists. The specified key was a different type so the method would always return false. For example (Let's assume the map is populated with objects that he wants to retrieve):
Map<String, String> map = new HashMap<String, String>();
Long value = new Long(12);
boolean hasString = map.containsKey(value);
My question is:
Why does the Map implementation force you to put keys of type K and values of type V, but the containsKey() method allow you specify any object?
There are other methods in the Map interface such as get() that also accept a parameter of type Object.