Java Hashmap返回引用而不是副本

Java Hashmap返回引用而不是副本,java,hashmap,Java,Hashmap,我有一个模型叫有财产的人 name image age amount 我有一个单例hashmaphashmap globalPersonList,其中包含person对象的列表 我试图从hashmap中检索一个对象,就像 Person existingPerson = globalPersonList.get("key"); 我想创建一个新的Person实例,并使用existingPerson属性进行初始化,如 Person person = new Person(); person =

我有一个模型叫有财产的人

name
image
age
amount
我有一个单例hashmap
hashmap globalPersonList
,其中包含person对象的列表

我试图从hashmap中检索一个对象,就像

Person existingPerson = globalPersonList.get("key");
我想创建一个新的
Person
实例,并使用
existingPerson
属性进行初始化,如

Person person = new Person();
person  =  globalPersonList.get("key");
现在我想将amount字段设置为这个person对象。我试着

newPerson.setAmount(100); 
但它不应该影响
globalPersonList
。我只想要newPerson对象中的金额值。但现在这也在
globalPersonList
中设置。设置数量后,如果我尝试

globalPersonList.get("key").getAmount()
它给出了我设定的数量。是否使用对新对象的引用?我想要Person对象的单独副本,这样它就不会影响主hashmap。

行中

Person person = new Person();
您正在创建一个新的Person对象,并将引用存储在变量
Person

public Person(Person sourcePerson) {
    //copy all field values (you didn't write what are your fields so I might not be 100% accurate here)
    this.name = sourcePerson.name;
    this.image = sourcePerson.image; //I don't know what type of data is stored in image so I'll just assume it's a String url path to an image
    this.age = sourcePerson.age;
    this.amount = sourcePerson.amount;
}
在下一行

person  =  globalPersonList.get("key");
您正在将该引用更改为HashMap中的Person对象之一。因此,您新创建的Person现在消失了,您对该对象所做的一切现在都会影响HashMap中的Person

相反,您应该使用HashMap中from对象的值初始化新Person对象,然后更改行中的值。

Person person = new Person();
您正在创建一个新的Person对象,并将引用存储在变量
Person

public Person(Person sourcePerson) {
    //copy all field values (you didn't write what are your fields so I might not be 100% accurate here)
    this.name = sourcePerson.name;
    this.image = sourcePerson.image; //I don't know what type of data is stored in image so I'll just assume it's a String url path to an image
    this.age = sourcePerson.age;
    this.amount = sourcePerson.amount;
}
在下一行

person  =  globalPersonList.get("key");
您正在将该引用更改为HashMap中的Person对象之一。因此,您新创建的Person现在消失了,您对该对象所做的一切现在都会影响HashMap中的Person


相反,您应该使用HashMap中的one from对象的值初始化新Person对象,然后更改一个值。

这是所需的行为。
Map的
get(…)
方法将返回存储在Map中的对象,而不是该对象的副本。您应该为您的
人员使用副本构造函数

public Person(Person sourcePerson) {
    //copy all field values (you didn't write what are your fields so I might not be 100% accurate here)
    this.name = sourcePerson.name;
    this.image = sourcePerson.image; //I don't know what type of data is stored in image so I'll just assume it's a String url path to an image
    this.age = sourcePerson.age;
    this.amount = sourcePerson.amount;
}
然后:

Person person = new Person(globalPersonList.get("key"));

这是我们想要的行为。
Map的
get(…)
方法将返回存储在Map中的对象,而不是该对象的副本。您应该为您的
人员使用副本构造函数

public Person(Person sourcePerson) {
    //copy all field values (you didn't write what are your fields so I might not be 100% accurate here)
    this.name = sourcePerson.name;
    this.image = sourcePerson.image; //I don't know what type of data is stored in image so I'll just assume it's a String url path to an image
    this.age = sourcePerson.age;
    this.amount = sourcePerson.amount;
}
然后:

Person person = new Person(globalPersonList.get("key"));

您需要从
HashMap
创建
Person
对象的副本。这可以通过复制构造函数完成:

public Person(Person person){
   // Setup with the same values as the passed in Person.
}
然后,您将使用此构造函数实例化一个新的
人员
,如下所示

Person newPerson = new Person(globalPersonList.get("key"));
这样做的原因是,当您调用
globalPersonList.get(“key”)
时,您将获得对堆上对象的引用。然后当您编写
newPerson=globalPersonList.get(“key”)
您实际上是说希望
newPerson
引用指向
globalPersonList.get(“key”)
指向的同一个对象


因此,如果在
newPerson
上更改值,实际上是在更改
HashMap
值指向的同一对象。

您需要从
HashMap
创建
Person
对象的副本。这可以通过复制构造函数完成:

public Person(Person person){
   // Setup with the same values as the passed in Person.
}
然后,您将使用此构造函数实例化一个新的
人员
,如下所示

Person newPerson = new Person(globalPersonList.get("key"));
这样做的原因是,当您调用
globalPersonList.get(“key”)
时,您将获得对堆上对象的引用。然后当您编写
newPerson=globalPersonList.get(“key”)
您实际上是说希望
newPerson
引用指向
globalPersonList.get(“key”)
指向的同一个对象


因此,如果在
newPerson
上更改值,实际上就是在更改
HashMap
值指向的同一对象。

更干净的方法将使用