-1

I'm creating a HashMap as follows:

private HashMap <String, JSONArray> HiveMap;
HiveMap = new HashMap <String, JSONArray>();

and also a private class variable to store some calculated data which will be put into the HashMap after the calculations are done:

private JSONArray hiveList;

After hiveList is calculated, I put the hiveList into the created HashMap (HiveMap):

HiveMap.put(hiveNode,hiveList);

When I print out HiveMap now, the output I get is as expected, with both the fields hiveNode and hiveList existing in the Hashmap. <"hiveNode":"hiveList">

After this is done, I assume that the data I put into the HashMap from hiveList is going to persist, so I clear the hiveList array using hiveList.clear();

But when I print the Hashmap after clearing hiveList, I see that the data from hiveList is gone from the HashMap as well.

Printing the HashMap after clearing hiveList results in: <"hiveNode": >

I don't understand this behavior, I would appreciate it if someone could shed some light on this.

Chaos
  • 9,543
  • 13
  • 37
  • 67

4 Answers4

8

The error is this:

After this is done, I assume that the data I put into the HashMap from hiveList is going to persist, so I clear the hiveList array using hiveList.clear();

What got passed to the hash map is the reference to the list, not a copy of it.

The most simple fix is to create a new instance instead of clearing it. Alternatively you can make a copy, and put that in the map.

kiheru
  • 6,493
  • 21
  • 29
3

The hiveList list and the list that resides as a value in the Map are one and the same, both the variable and the Map are holding references to the same object; if you clear() one list, the other is also emptied - there's a single list, not two.

If you have to clear and reuse hiveList for some reason (and I don't see why), then you'll need to put a different list in the Map, here's how to do a shallow copy:

HiveMap.put(hiveNode, new ArrayList<Hive>(hiveList));

Replace <Hive> with the actual type of the elements in the list.

Óscar López
  • 215,818
  • 33
  • 288
  • 367
3

In java objects are passed by copy of reference and not copy of object. So if you modify the object using reference at one place it get affected at all the places where this object is referred. In you case hiveList.clear();
is causing to remove all the elements of the list which is also referred at HiveMap.put(hiveNode,hiveList);and hence is eventually removed from the map.

Ankur Shanbhag
  • 7,376
  • 2
  • 26
  • 37
2

That's how java manages the Object. What you have is the reference to that object and you only stored a reference to it. So, if you clear the object after storing it in the hashmap, the object from hashmap would also be cleared.

When you create an object java stores the reference along with the object at the same location in the memory. Hence, there's only one reference to that object. So, the result was expected

//Stores the reference to the object
HiveMap.put(hiveNode,hiveList);

//Modifies the same object in the memory
hiveList.clear();

You may want to read about Pass by reference and Pass by Value

Raunak Agarwal
  • 6,637
  • 6
  • 34
  • 60