hashmap - Can you explain this Java hash map key collision? -


i have hashmap , used in following way:

hashmap<someinterface, uniqueobject> m_map;  uniqueobject getuniqueobject(someinterface keyobject) {      if (m_map.containskey(keyobject))      {          return m_map.get(keyobject);      }      else      {          return makeuniqueobjectfor(keyobject);      } } 

my issue i'm seeing multiple objects of different classes matching same key on m_map.containskey(keyobject).

so here questions:

  1. is possible? map interface says uses equals() compare if key not null. haven't overridden equals() in of someinterface classes. mean equals method can wrong?

  2. if above true, how hashmap return true on equals() if in fact same object , not copy? possible saying if (object1 == object2)? told on in java development should avoid doing that, never found out when should used.

thanks in advance. :)

i suspect you've misdiagnosed issue. if aren't overriding equals anywhere (and you're not subclassing else overrides equals) should indeed have "identity" behaviour.

i shocked hear not case, honest.

if can product short complete program demonstrates problem, make easier - moment, i'd definitely double-check suspicions seeing different objects being treated equal keys.


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -