Java HashMap更改值对象

Java HashMap更改值对象,java,hashmap,Java,Hashmap,我有一个字符串集,我想用字符串键和节点对象值创建哈希映射。 这是我的密码 Set<String> cities = new HashSet<>(); Map<String, Node> allCity = new HashMap<>(); Iterator<String> c = cities.iterator(); while(c.hasNext()){ String name = c.next();

我有一个字符串集,我想用字符串键和节点对象值创建哈希映射。 这是我的密码

Set<String> cities = new HashSet<>();
Map<String, Node> allCity = new HashMap<>();
Iterator<String> c = cities.iterator();
while(c.hasNext()){
                String name = c.next();
                Node cit = new Node(name);
                allCity.put(name, cit);
            }
Set cities=new HashSet();
Map allCity=newhashmap();
迭代器c=cities.Iterator();
while(c.hasNext()){
字符串名称=c.next();
节点cit=新节点(名称);
allCity.put(名称,cit);
}
我的问题是,当我从c迭代器读取第一个对象并正确地创建新对象并将其放入哈希映射时,但当在哈希映射中创建第二个对象时,前面的对象值发生了如下更改

初读 key=“纽约” Value=Node(Node的值为newyork)

二读 Key=“洛杉矶” 值=节点(节点值为视线角) 我在纽约的第一个读数是改成洛杉矶

myNode类

public class Node{
private static String city;
private static double pathCost;
private ArrayList<Edge> neighbours;
private Node parent;

public Node(String cityName){
    city = cityName;
    neighbours = new ArrayList<>();
}

public static String getValue() {
    return city;
}
public static void setValue(String city) {
    Node.city = city;
}
public static double getPathCost() {
    return pathCost;
}
public static void setPathCost(double pathCost) {
    Node.pathCost = pathCost;
}

public static String getCity() {
    return city;
}

public static void setCity(String city) {
    Node.city = city;
}

public ArrayList<Edge> getNeighbours() {
    return neighbours;
}

public  void setNeighbours(ArrayList<Edge> neighbours) {
    this.neighbours = neighbours;
}

public void addNeighbours(Edge n){
    this.neighbours.add(n);
}

public Node getParent() {
    return parent;
}
public void setParent(Node parent) {
    this.parent = parent;
}

@Override
public String toString() {
    return city;
}
公共类节点{
私人静态字符串城市;
私有静态双路径成本;
私人ArrayList邻居;
私有节点父节点;
公共节点(字符串cityName){
城市=城市名称;
邻居=新的ArrayList();
}
公共静态字符串getValue(){
回归城市;
}
公共静态无效设置值(字符串城市){
Node.city=城市;
}
公共静态双getPathCost(){
返回路径成本;
}
公共静态无效setPathCost(双路径成本){
Node.pathCost=路径成本;
}
公共静态字符串getCity(){
回归城市;
}
公共静态无效设置城市(字符串城市){
Node.city=城市;
}
公共ArrayList getneights(){
返回邻居;
}
公共邻居(ArrayList邻居){
这个。邻居=邻居;
}
公共区域(边缘n){
本.邻居.加(n);
}
公共节点getParent(){
返回父母;
}
公共void setParent(节点父节点){
this.parent=parent;
}
@凌驾
公共字符串toString(){
回归城市;
}
}


请帮帮我。

那是因为您创建了
城市
(和
路径成本
)字段
静态
。静态字段属于该类,而不是该类的特定实例。每个节点都有一个特定的城市,因此您希望将城市字段定义为实例字段,而不是静态字段


阅读。

这是因为您创建了
city
(和
pathCost
)字段
static
。静态字段属于该类,而不是该类的特定实例。每个节点都有一个特定的城市,因此您希望将城市字段定义为实例字段,而不是静态字段


阅读。

未仔细阅读,此处有一个重大错误:

private static String city;
city
是节点(即实例)数据,不应是静态的


因为在您的情况下它是静态的,所以所有节点都为
city
共享一个值,这很可能不是您想要的。同样的情况也适用于
pathCost

如果没有仔细查看,这里有一个重大错误:

private static String city;
city
是节点(即实例)数据,不应是静态的


因为在您的情况下它是静态的,所以所有节点都为
city
共享一个值,这很可能不是您想要的。这同样适用于
pathCost

节点
类中的
city
成员是
static
。这意味着所有节点
共享相同的
城市
,当一个实例更新它时(例如,在构造函数中),更改适用于所有节点

要解决此问题,您可以将
城市
更改为实例成员:

public class Node{
    private String city;
    ...

节点
类中的
城市
成员是
静态
。这意味着所有节点
共享相同的
城市
,当一个实例更新它时(例如,在构造函数中),更改适用于所有节点

要解决此问题,您可以将
城市
更改为实例成员:

public class Node{
    private String city;
    ...

您可能希望删除代码段中的static关键字(因为您告诉他如何解决此问题):@thomasgrr。。。显然,我采用的是复制和bug设计模式。谢谢这是最好的模式后,复制和面食;)您可能希望删除代码段中的static关键字(因为您告诉他如何解决此问题):@thomasgrr。。。显然,我采用的是复制和bug设计模式。谢谢这是最好的模式后,复制和面食;)附带说明:如果您想改进代码,请对(字符串名称:cities)allCity.put(name,newnode(name))进行如下更改
只是一个旁注:如果您想改进代码,请对(字符串名称:cities)allCity.put(name,newnode(name))进行
更改是的,谢谢你修复它。:)是的,谢谢你修好了