java - retrieval of duplicate keys in hashmap -


since duplicate key overrides previous key , corresponding value in hashmap. if call get() method , provide previous key argument, returns overriden value. how possible since key overriden new key should throw exception.

//this class used key class mapkey {   public boolean equlas( object o)   {     return true;   } }  //this test class class maptest {   public static void main(string a[])   {     map m=new hashmap<mapkey, string>();     mapkey mk1=new mapkey();     m.put(mk1,"one");      mapkey mk2=new mapkey();     m.put(mk2,"two");      mapkey mk3=new mapkey();     m.put(mk3,"three");      system.out.println(m.get(mk1));     system.out.println(m.get(mk2));   } } 

output: 3 three

since keys equal should overriden last key object mk3. how possible retrieve value first or second key object?

thnks in advance

this because have not overrided hashcode method. in equals method have declared objects equal. due default hashcode implementation equal objects returning different hashcodes. hashcodes used save values in hashmap, able values old keys mk1 , mk2.


Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

order - Notification for user in user account opencart -