A quick question which maybe is lame.
In the following code:
Map<Integer, Double[]> dataMap = new Map<Integer, Double[]>();
dataMap.put(1, new Double[]{100,100});
Double[] dob = dataMap.get(1);
dob[0] = 100;
dob[1] = 200;
dataMap.put(1, dob);
Is the last “dataMap.put” instruction necessary? or will the dataMap.get(1) yield a reference to the array which will then be modified directly in the later statements.
I know that, in the case of mutable objects (e.g. Map), Map.get() will give me the reference to the desired object, however with an array of Doubles (whose element type e.g. Double are immutable) I am not sure if I get a reference to the array in the Map.
Thanks!
No, the final statement isn’t necessary – because the map only contains references to arrays, as you’ve mentioned. An array is a mutable object, even though
Doubleisn’t – it’s like having an object with asetName(String)method – just becauseStringis immutable, the container type isn’t.Note that if you do this, another thread1 may see half the change (i.e. the setting of the first element to 100) without seeing the second element set to 200. Is that okay? If not, you could think about creating a new array instead:
1 This is assuming you’re using a thread-safe map to start with, of course…