1

I've been working with HashMaps when I came to a question which I couldn't solve myself or find on the web.

HashMap<String,HashMap<String,String>> m= new HashMap<>();
HashMap<String,String> t = new HashMap<>();

t.put("test1","1");
m.put("h1",t);

t = new HashMap<>();
t.put("test2,"2");
m.put("h2",t);

System.out.println(m);

That gives me {h1={test1=1}, h2={test2=2}} Thus the big HashMap contains data of both HashMaps. So the question is did it simply copy the data of smaller HashMaps, or do both "t" HashMaps stay in JVM memory, and HashMap m simply links me to them?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
black
  • 357
  • 3
  • 18
  • #2 is the answer. the data was not copied, m simply points to both instances of the hash map. the fact that t doesn't point to the previous hashmap instance doesn't make it go away if someone else points to it (m) – TheZuck Apr 18 '13 at 05:27
  • [read this](http://javarevisited.blogspot.com/2011/04/garbage-collection-in-java.html) and then read some more. – Dariusz Apr 18 '13 at 05:31

5 Answers5

1

Your big HashMap contains a reference to both HashMaps. What you've done is changed what HashMap t is pointing too.

So as a more visual example lets say

t = 0x00000001

when you put t into m, m now looks like

m = {0x00000001}

at this point both t and m have references to the same HashMap, meaning any change to one will appear in the other. Now when you go t = new HashMap<>() you are actually reassigning the reference that t points to

t = 0x00000002

but m still looks like {0x00000001} so you didn't lose your first reference. And when you put t into m the second time they look like

t = 0x00000002
m = {0x00000001, 0x00000002}

so in the end, m still contains a reference to both HashMaps

TheMerovingian
  • 648
  • 4
  • 14
0

Whenever you place t into m, no copy of the data is made. Instead, a reference to the sub-map is placed.

You add t to m twice. However, each time t points to a different object. Thus you end up with two separate, independent sub-maps in m.

Contrast this with the following example:

      t.put("subkey","old");
      m.put("h1",t);
      m.put("h2",t);
      t.put("subkey", "new");
      System.out.println(m);

This prints out

{h1={subkey=new}, h2={subkey=new}}

Here, t and both keys of m point to the same sub-map. When you change one, they all change.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
0

Map coutains reference of your Object. It internally contains array bucket of Entry class which holds key,value object reference.

HashMap m will contains object reference of your first created hashmap against h1 and second hashmap's reference aginst h2.

Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103
0

Everything in Java is a pointer or reference, so the objects are held in memory and the hash map is storing references, not copying the actual data.

The big hash map contains a reference to the smaller ones so the garbage collector won't free up the memory.

Alex
  • 1,993
  • 1
  • 15
  • 25
0

Both HashMap will stay in memory. You could refer them through the hashmap m with key h1 and h2. The data wont be copied from hashmaps but the reference of the object is changed to hashmap key(i.e. h1 & h2).

Jaydeep Rajput
  • 3,605
  • 17
  • 35