hashmap - Objects in java. What happens with them after they are no more assigned to a variable? -
this question has answer here:
- what garbage collector in java? 17 answers
i've been working hashmaps when came question couldn't solve myself or find on 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}} big hashmap contains data of both hashmaps. question did copy data of smaller hashmaps, or both "t" hashmaps stay in jvm memory, , hashmap m links me them?
whenever place t m, no copy of data made. instead, reference sub-map placed.
you add t m twice. however, each time t points different object. end 2 separate, independent sub-maps in m.
contrast 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 , both keys of m point same sub-map. when change one, change.
Comments
Post a Comment