You need to sign in to do that
Don't have an account?
Edweek Shane
Using the same object as the value for two different keys in a map
Hi everyone,
I just spent a couple of hours trying to debug this one, and now that I have, I'm not sure why my solution is working...and that's just not very satisfying.
I'm populating the values of map<ID, map<string, double>> inside a loop. I'll refer to this one as "outerMap", and map<string, double> as "innerMap."
First, I check for the existence of an ID in outerMap's keyset.
- If it's there, I update the values in innerMap like this: outerMap.get(ID).put(string, double)
- If it isn't there, I clear the values in innerMap, (re)populate the values, and then add the entry to outerMap like this: outerMap.put(ID, innerMap)
The data I'm testing with makes it so that there are two objects in the list that have the same value for the ID I'm testing for, and one that has a different ID. So, the loop runs like this:
[0] -- ID isn't found in outerMap's keyset. Add an entry to outerMap.
[1] -- ID is found. Update the existing entry in outerMap.
[2] -- ID isn't found. Add an entry to outerMap.
Here's what I'm seeing:
[0] -- The entry is added with no problem.
[1] -- The entry is updated with no problem.
[2] -- When the entry is added, the values inside innerMap are the same as they were in [0], despite having been changed during this loop. I can see in the debug logs that the values in innerMap have indeed been changed. (That was freakin' maddening!)
I put in a debug message just before the code that added the new entry to outerMap:
system.debug('outerMap = ' + outerMap);
During loop [0], the output was: outerMap = {ID_1={key_1=value_1, key_2=value_2, key_3=value_3}}
During loop [2], the output was: outerMap = {ID_1={key_1=value_1, key_2=value_2, key_3=value_3}, ID_2=(already output)}
It seems like what's happening is that, even though the values have changed, the fact that I'm using the same instance of innerMap as I did the first time through, is making it so that the values for the new outerMap key added in [2] are the same as the values for the outerMap key added in [0]. When I create a new instance of innerMap instead of using innerMap.clear(), everything works great...but I don't understand why.
Anyone care to help me out with a (now) strictly academic question?
I just spent a couple of hours trying to debug this one, and now that I have, I'm not sure why my solution is working...and that's just not very satisfying.
I'm populating the values of map<ID, map<string, double>> inside a loop. I'll refer to this one as "outerMap", and map<string, double> as "innerMap."
First, I check for the existence of an ID in outerMap's keyset.
- If it's there, I update the values in innerMap like this: outerMap.get(ID).put(string, double)
- If it isn't there, I clear the values in innerMap, (re)populate the values, and then add the entry to outerMap like this: outerMap.put(ID, innerMap)
The data I'm testing with makes it so that there are two objects in the list that have the same value for the ID I'm testing for, and one that has a different ID. So, the loop runs like this:
[0] -- ID isn't found in outerMap's keyset. Add an entry to outerMap.
[1] -- ID is found. Update the existing entry in outerMap.
[2] -- ID isn't found. Add an entry to outerMap.
Here's what I'm seeing:
[0] -- The entry is added with no problem.
[1] -- The entry is updated with no problem.
[2] -- When the entry is added, the values inside innerMap are the same as they were in [0], despite having been changed during this loop. I can see in the debug logs that the values in innerMap have indeed been changed. (That was freakin' maddening!)
I put in a debug message just before the code that added the new entry to outerMap:
system.debug('outerMap = ' + outerMap);
During loop [0], the output was: outerMap = {ID_1={key_1=value_1, key_2=value_2, key_3=value_3}}
During loop [2], the output was: outerMap = {ID_1={key_1=value_1, key_2=value_2, key_3=value_3}, ID_2=(already output)}
It seems like what's happening is that, even though the values have changed, the fact that I'm using the same instance of innerMap as I did the first time through, is making it so that the values for the new outerMap key added in [2] are the same as the values for the outerMap key added in [0]. When I create a new instance of innerMap instead of using innerMap.clear(), everything works great...but I don't understand why.
Anyone care to help me out with a (now) strictly academic question?
I think it's because the value for both keys is the innerMap object, not the contents of the innerMap object. That explains why loop [2] seemed to be overwriting what I (thought I) put in during loop [0]. Makes sense -- it is the same object! ((Head smack.))