I was surprised when I saw that when I cleaned the internal map, then after adding it to an external map. Is there any way to reset the inner map, but not to remove objects from the map of the outside?
Main category:
Public static zero main (string array []) {person per person = new person ("1", "AA", 56, true) ; Person = 2 new person ("20", "BBB", 34, wrong); Map and lieutenant; Integer, map & lt; Integer, person & gt; & Gt; External = new hashmop & lt; Integer, map & lt; Integer, person & gt; & Gt; (); Maps & lt; Integer, person & gt; Internal = new hashmap & lt; Integer, person & gt; (); Internal. Input (100, per 1); Internal. Input (200, 2); External Input (1000, internal); // Internal: Size = 2, External: Size = 1 (Key = 1000, Value Size = 2) Inner clear (); // Internal: Size = 0, External: Size = 1 (Key = 1000, Size of value = 0) // External value Size = 0 Why? }
person:
public class person {private string id; The name of the private string; Personal gaps ages; Private boolean is something; Public string getId () {return ID; } Public Zero Set ID (string id) {this.id = id; } Public string getName () {return name; } Public Zero Setname (string name) {this.name = name; } Public int getAge () {return age; } Public Zero setAge (int age) {this.age = age; } Public boolean is something () {return is something; } Public Zero Set is something (Boolean is Paid) {this.isSome = isPaid; } Public person () {} public person (string id, string name, int era, boolean something) {this.id = id; This.name = name; This.age = age; This.is something = something; }}
external.product (1000, internal);
In the external code, the reference in the internal
variable as a value does not make a copy of the example specified by inside
, so if you later Inner
clean the map, you also clear the stored value in the external map.
If you want to copy the original map's value to the original inner map, then you can create a new instance:
outer.put (1000 , New hashmap (internal));
Note that in this case, in the new inner map, the person
examples will have the same example as the original person
examples. > Internal map, so if you change one of these, the example stored in the map will also be mutated.
No comments:
Post a Comment