Java - the best way to update a HashMap value is to pass the value / reference
I have a HashMap with some data. Looking at the following code ...
HashMap<String, Double[]> map; //Already populated with data
Double[] results = map.get(key);
updateArray(results); //This function makes changes to the results array.
map.put(key, results);
... my question is, is map.put (key, results) needed?
I am still a little confused about the pass-by-value and pass-by-reference Java. To be clear, in the first line of code we get a double array reference, right? So the function on the second line should properly update the Double array in the HashMap ... which would then seemingly make the map.put () on the third line redundant.
Looking at the other HashMap related code, they always use the put () method. I just wanted to make sure there are no unintended consequences for doing this without a put () method.
Thanks for any input!
source to share
Map.get(Object)
just returns a reference to the specified array, it does not copy the contents of the array to a new array. Therefore, any changes you make to the array returned Map.get(Object)
will show up in what is stored in Map
, because they are the same array . Therefore, this makes the challenge Map.put(Object,Object)
in this situation completely redundant.
source to share
map.put(key, results)
only necessary if it map.get(key)
returns null, as in that case you have to create a new array and place it on the map.
If map.get(key)
returns a non-null array, updateArray(results)
this array will be updated and there is no need to put the same array in again Map
.
So, to summarize, this code covers all cases:
HashMap<String, Double[]> map; //Already populated with data
...
Double[] results = map.get(key);
if (results == null) {
results = new Double[someLength];
map.put(key, results);
}
updateArray(results);
Now, if your Map's value was an immutable class like String or Integer, you would need to put a new instance on the Map in order to replace it as you won't be able to change the existing value.
source to share
In the shown code, this is completely unnecessary. The return value from is map.get(key)
stored in a variable results
and is results
never subsequently assigned. Therefore, the value results
passed to map.put()
must be the same as the value passed from map.get(key)
. This is true regardless of the type.
There is a slight difference with calling map.put(key, results)
and not calling it in this case - if results
(result map.get(key)
) is equal null
, map.put(key, results)
will put the value null
into it before. ( HashMap
allows values null
.)
source to share
updateArray(results);
This will update the result object (which contains a collection of an immutable object, which is a String object), which means that when you update the value of the object (results / String), it will create another string under the result object reference. Therefore, if you want to see a change in the map than again, you need to put it on the map.
my question is whether or not the map.put(key, results) is even necessary?
map.put(key,results); // will only reflect the changes in map
source to share