I'm confused about what happens when i change part of the key value of a map. For instance:
account a = new account(name = 'account name', website = 'www.testdomain.com');
map<account, string> someMap = new map <account, string>();
someMap.put(a, 'tra la li la');
Now let's change the website and see what happens.
a.website = 'www.salesforce.com';
system.debug('a: ' + a + '\n' + 'someMap: ' + someMap);
if(someMap.containsKey(a)){system.debug('key found' );}
else{system.debug('key isnt found');}
Debug result:
a: Account:{Name=acount name, Website=www.salesforce.com}
someMap: {Account:{Name=account name, Website=www.salesforce.com}=null}
Key isnt found
As i understand this, the account is passed by reference and therefore the website in the map has also changed. However, why does this change the string value to null? Also why doesn't the map contain the account key anymore, even though both the original account and the map key have both been changed in exactly the same way?
Now let's put in the same account once more:
someMap.put(a, 'tra la li la');
system.debug('a: ' + a);
system.debug'someMap: ' + someMap);
if(someMap.containsKey(a)){system.debug('key found' );}
Debug result:
a: Account:{Name=account name, Website=www.salesforce.com}
someMap: {Account:{Name=account name, Website=www.salesforce.com}=tra la li la}
key found
Before, i was told the key value doesn't exist, but now when i add the account again, it overwrites the original key instead of adding an extra value. Why does it do that?
I find this behavior confusing. I'd like to understand the logic behind it.