Java - Best way to update HashMap value - Pass by value/reference related
Asked Answered
P

7

6

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 whether or not the map.put(key, results) is even necessary?

I am still a little confused about the pass-by-value and pass-by-reference nature of Java. To be clear, in the first line of code, we are getting a reference to the Double array, correct? As such, 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 other people's HashMap related code, they always seem to be using the put() method. I just wanted to make sure that there aren't any unforeseen consequences of doing it without the put() method.

Thanks for any input!

Pegram answered 20/8, 2014 at 15:1 Comment(7)
It's generally not a good idea to mix Collections and arrays.Logway
What happened when you wrote a little test program to find out? As for your confusion with pass-by-value vs pass-by-reference (which Java doesn't even support), I highly recommend reading this and then this.Koffman
@BackSlash Map is a type of collection, HashMap is an implementation of it.Logway
The examples you are observing where "put" is being utilized are probably storing immutable values. They are replacing them as opposed to modifying them which will require another call to "put"Malissamalissia
@DrewBuckley Not quite sure what you're trying to say here. Why is another put() call needed here at all?Pegram
@Pegram My point was addressing your observation of other people's use of HashMap. You stated "they always seem to be using the put() method." This is most likely because the values stored in their HashMap are immutable, meaning they cannot be altered. Objects can be derived from them maybe, but that actual object in memory can not be changed. For example, if we wanted to "modify" a String stored in the HashMap, we could derive another String from it, but ultimately we would have to call "put" to replace the original String.Malissamalissia
@DrewBuckley Thanks for the clarification. I misunderstood and thought you were referring to my code for some reason. Probably because it's 4 am here!Pegram
G
3

If you're modifying the object referenced by the reference value that you retrieved from the HashMap, there is no point replacing its entry in that same HashMap.

If you're modifying the reference, then you do need to replace it.

Gentianella answered 20/8, 2014 at 15:2 Comment(0)
M
3

Map.get(Object) simply returns a reference to said array, it does not copy the array's contents to a new array. Therefore any changes you make to the array returned by Map.get(Object) will be reflected in the one stored in the Map because they are the same array. This therefore makes calling Map.put(Object,Object) in this situation completely redundant.

Malissamalissia answered 20/8, 2014 at 15:7 Comment(0)
B
1

map.put(key, results) is necessary only if map.get(key) returns null, since in that case you should create a new array and put it in the map.

If map.get(key) returns a non-null array, updateArray(results) would update that array and there's no need to put the same array again into the 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 the value of your Map was an immutable class such as String or Integer, you would have to put a new instance in the Map in order to replace it, since you wouldn't be able to change the existing value.

Betteann answered 20/8, 2014 at 15:2 Comment(0)
F
0

In the code shown, it is completely unnecessary. The return value from map.get(key) is stored in the variable results, and results is never assigned to afterwards. Therefore, the value of results passed to map.put() is guaranteed to be the same as the one gotten from map.get(key). This is true regardless of type.

There is a slight difference with calling map.put(key, results) and not calling it in this case -- if results (result of map.get(key)) is null, map.put(key, results) will put a null value in that wasn't there before. (HashMap allows null values.)

Factoring answered 20/8, 2014 at 18:39 Comment(0)
I
0
updateArray(results);

This will update the results object(which is contains collection of immutable object which is String object) that means when you update the value of (results / String) object it will create another String under the results object reference. So if you want to see the change under map than again you have to put it in 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
Ivanivana answered 21/8, 2014 at 8:26 Comment(0)
A
0
HashMap<String,String> map = new HashMap<>();
map.put("key", "value");

RestAssured.baseURI = BASE_URL;
RequestSpecification request = RestAssured.given();
request.auth().preemptive().basic("Username", "Password").body(map).put("url");
System.out.println("The value of the field after change is: "  + map.get("key"));
Andri answered 11/10, 2020 at 10:49 Comment(1)
Hello, it would be great if you explain your answer regarding how/why this is a ideal solution to the given question, beside posting just some source code.Roumell
F
0

Consider the following example.

Map<String, List<Double>> map = new HashMap<>();

List<Double> list1 = new ArrayList<>(); 
list1.add(1.0); 
list1.add(2.0);

map.put("1", list1);

Now, if you do the following, the map will automatically update. This is because map holds a reference to the actual list object. When we update the related list, map will see that change without any updates to the map.

List<Double> list2 = map.get("1");
list2.add(10.0);
System.out.println(map);

Above print statement will give the following output, which will answer the question.

{1=[1.0, 2.0, 10.0]}
Formenti answered 11/9, 2021 at 7:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.