简体   繁体   English

HashMap更改Values对象

[英]HashMap change Values Object

I have a set with String and i want to create hash map with String key and Node Object value. 我有一个String集合,我想用String键和Node Object值创建哈希映射。 and this is my code 这是我的代码

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);
            }

my problem is when i read first from c iterator and correctly make new object and put it to hash map but when second object was create in my hash map the previous object value was change like this 我的问题是,当我先从c迭代器读取并正确创建新对象并将其放入哈希映射时,但是当我在哈希映射中创建第二个对象时,先前的对象值会像这样更改

first read key = "New York" Value = Node (and the value of node is New York) 第一个读取键=“纽约”值=节点(节点的值为纽约)

second read Key = "Los Angles" Value = Node (and the value of node is Los Angles) and my first read Value with New York key was change to Los Angles. 第二次读取的Key =“ Los Angles”值=节点(节点的值为Los Angles),而我对纽约键的第一个读取的Value是更改为Los Angles。

myNode class 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;
}

} }

Please help me. 请帮我。

That's because you made the city (and pathCost ) fields static . 这是因为您将city (和pathCost )字段设置为static A static field belongs to the class, not to a specific instance of this class. 静态字段属于该类,而不属于该类的特定实例。 Each node has a specific city, so you want to mek the city field an instance field, and not a static field. 每个节点都有一个特定的城市,因此您要使city字段成为实例字段,而不是静态字段。

Read the Java tutorial about class members . 阅读有关类成员Java教程

The city member in your Node class is static . 您的Node类中的city成员是static This means all the Node s share the same city , and when one instance updates it (eg, in the constructor), the change applies for all of them. 这意味着所有 Node共享同一个city ,并且当一个实例更新它(例如,在构造函数中)时,更改将应用​​于所有城市。

To resolve this issue, you could change city to be an instance member: 要解决此问题,您可以将city更改为实例成员:

public class Node{
    private String city;
    ...

Without looking thoroughly there is a major mistake here: 如果不仔细看,这里有一个主要错误:

private static String city;

city is node (ie instance) data and should not be static. city是节点(即实例)数据,不应为静态数据。

Since it is static in your case, all nodes share one value for city , which most probably isn't what you want. 由于在您的情况下是静态的,因此所有节点都为city共享一个值,这很可能不是您想要的。 The same applies to pathCost . 同样适用于pathCost

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM