My first instinct is to say each key is an object, and has a hash code, which is what is used to determine if a duplicate is being inserted. However, I can’t find anything to back that up for sure. Can someone provide a link that says that, or provide the real answer here? Thanks!
Share
The
Mapinterface specifies that if two keys arenullthey are duplicates, otherwise if there’s a keyksuch thatkey.equals(k), then there is a duplicate. See the contains or get method here:http://java.sun.com/javase/6/docs/api/java/util/Map.html#containsKey(java.lang.Object)
However, it’s up to the
Mapimplementation how to go about performing that check, and aHashMapwill use a hash code to narrow the potential keys it will check with theequalsmethod. So in practice, for a typical hash based map, to check for duplicates a map will take the hashcode (probably mod some size), and useequalsto compare against any keys whose hashcode mod the same size gives the same remainder.