I have this below code where I create a map with sobject as key and integer as value. I added 2 account sobject keys. Size is 2, and I later changed one of its names to the other one so that now they are duplicated.
Ideally map since it's a reference to sobject, changes should be reflected and map size should shrink to one. In my code, it does only if i do a system.debug of map before i calculate size/assert. It's the same behavior for set as well. Why is that so?
map<account,integer> mymap = new map<account,integer>();
account a = new account(name='apple');
mymap.put(a,1);
system.assertequals(mymap.size(),1);
mymap.put(new account(name='orange'),1);
system.assertequals(mymap.size(),2);
a.name='orange';
system.debug(mymap);//if i remove this my assertion will fail and size will be displayed as 2.if i add this statement before ,size is 1 and assertion succeeds
system.debug(mymap.size());
system.assertequals(mymap.size(),1);