簡體   English   中英

Java:不可能使用迭代器來迭代對象的HashMap並更改對象的屬性嗎?

[英]Java: Is it not possible to use an iterator to iterate a HashMap of objects and change attributes of objects?

我知道這對這里的專家來說可能是一個非常基本的問題。 但是我無法解決這個問題。 基本上,我有以下深度優先搜索實例方法。 我有一個對象類Dgraph ,它是HashMap<Vertex, ArrayList<Vertex>> Vertex只是我創建的對象類,它包含一個color屬性和一個node屬性。

問題:基本上,我想更改iterator語句中Vertex對象的顏色。 但是它沒有按預期工作。 我能夠更改迭代器中每個頂點對象的color屬性,但是當我離開迭代器時,每個頂點的color屬性仍然為null

注意,具有相同節點值的Vertex對象是同一對象 ,即考慮5 --> 11 -- 9 ,這里我們只有一個Vertex對象,其節點等於1

public void depth_first_search(Dgraph graph){
        // for debugging
        System.out.println("Before iterator");
        System.out.println("Graph = " + graph);
        System.out.println("");
        graph.display();
    HashMap<Vertex, ArrayList<Vertex>> dag = graph.returnHashMap(graph);
    for(Map.Entry<Vertex, ArrayList<Vertex>> g_map : dag.entrySet() ){
        Vertex u = g_map.getKey();
        u.set_color("WHITE");
        System.out.println("In iterator --> Vertex u = " + u + " color = " + u.get_color());
    }
    // for debugging 
    System.out.println("*************************");
    System.out.println("After iterator");
    System.out.println("Graph = " + dag);
    graph.display();
    }
}

這是我得到的調試輸出:

Before iterator
Graph = Dgraph@7852e922

5 : [1, 9]
    null    null
3 : [511596]
    null
2 : [47646, 47647, 13019, 47648, 47649, 47650, 7700, 47651, 47652]
    null    null    null    null    null    null    null    null    null
1 : [1, 2, 5, 6, 7, 3, 8, 4]
    null    null    null    null    null    null    null    null
in iterator --> Vertex u = 5 color = WHITE
in iterator --> Vertex u = 3 color = WHITE
in iterator --> Vertex u = 2 color = WHITE
in iterator --> Vertex u = 1 color = WHITE
*************************
After iterator
Graph = {5=[1, 9], 3=[511596], 2=[47646, 47647, 13019, 47648, 47649, 47650, 7700, 47651, 47652], 1=[1, 2, 5, 6, 7, 3, 8, 4]}

5 : [1, 9]
    null    null
3 : [511596]
    null
2 : [47646, 47647, 13019, 47648, 47649, 47650, 7700, 47651, 47652]
    null    null    null    null    null        null    null   null   null
1 : [1, 2, 5, 6, 7, 3, 8, 4]
    WHITE   null        null    null    null    null    null       null

我刪除了toString方法,因此你們可以看看引用,並檢查其中的一部分引用同一對象。

Before iterator
Graph = Dgraph@7852e922

Vertex@9b : [Vertex@1f, Vertex@117]
    null    null
Vertex@5d : [Vertex@f1ff14]
    null
Vertex@3e : [Vertex@1689a2, Vertex@1689c1, Vertex@62885, Vertex@1689e0, Vertex@1689ff, Vertex@168a1e, Vertex@3a46c, Vertex@168a3d, Vertex@168a5c]
    null    null    null    null    null    null    null    null    null
Vertex@1f : [Vertex@1f, Vertex@3e, Vertex@9b, Vertex@ba, Vertex@d9, Vertex@5d, Vertex@f8, Vertex@7c]
    null    null    null    null    null    null    null    null
in iterator --> Vertex u = Vertex@9b color = WHITE
in iterator --> Vertex u = Vertex@5d color = WHITE
in iterator --> Vertex u = Vertex@3e color = WHITE
in iterator --> Vertex u = Vertex@1f color = WHITE
*************************
After iterator
Graph = {Vertex@9b=[Vertex@1f, Vertex@117], Vertex@5d=[Vertex@f1ff14], Vertex@3e=[Vertex@1689a2, Vertex@1689c1, Vertex@62885, Vertex@1689e0, Vertex@1689ff, Vertex@168a1e, Vertex@3a46c, Vertex@168a3d, Vertex@168a5c], Vertex@1f=[Vertex@1f, Vertex@3e, Vertex@9b, Vertex@ba, Vertex@d9, Vertex@5d, Vertex@f8, Vertex@7c]}

Vertex@9b : [Vertex@1f, Vertex@117]
    null    null
Vertex@5d : [Vertex@f1ff14]
    null
Vertex@3e : [Vertex@1689a2, Vertex@1689c1, Vertex@62885, Vertex@1689e0, Vertex@1689ff, Vertex@168a1e, Vertex@3a46c, Vertex@168a3d, Vertex@168a5c]
    null    null    null    null    null    null    null    null    null
Vertex@1f : [Vertex@1f, Vertex@3e, Vertex@9b, Vertex@ba, Vertex@d9, Vertex@5d, Vertex@f8, Vertex@7c]
    WHITE   null    null    null    null    null    null    null

更新:實例方法returnHashMap位於類Dgraph ,如下所示:

class Dgraph{

    // instance variable
    HashMap<Vertex, ArrayList<Vertex>> dag;

    // constructor
    public Dgraph(String file, boolean reverse) throws IOException{
        dag = read_file_and_populate(file, reverse);
    }

    public HashMap<Vertex, ArrayList<Vertex>> returnHashMap(Dgraph g){
        return g.dag;
    }
    .....
}

Vertex類如下所示:

class Vertex{
    private long node;
    private String color;
    private long d;
    private long pi;
    private long f;

public Vertex(long node){
    this.node = node;
}

// return color
public String get_color(){
    return color;
}

// change color
public void set_color(String color){
    this.color = color;
}

// return distance
public long get_d(){
    return d;
}

// change distance
public void set_d(long d){
    this.d = d;
}

// return predecessor
public long get_pi(){
    return pi;
}

// assign predecessor
public void set_pi(long pi){
    this.pi = pi;
}

// to String (prevent print reference)
public String toString(){
    return node+"";
}

// return node
public long get_node(){
    return node;
}

// return finishing time
public long get_f(){
    return f;
}

// set finishing time
public void set_f(long f){
    this.f = f;
}

// Need hashCode() and equals() to compare objects
 public int hashCode(){
     return (int)(node * 31);
}

public boolean equals(Object o) {
    if (o == this){
        return true;
    }
    if (o == null || getClass() != o.getClass()){
        return false;
    }
    Vertex other = (Vertex)o;
    return node  == other.node;
}
}

這是我正在使用的顯示方法:

public void display(){
    for(Map.Entry<Vertex, ArrayList<Vertex>> entry : dag.entrySet()){
        System.out.println(entry.getKey() + " : " + entry.getValue());
        for(Iterator<Vertex> iterator = dag.get(entry.getKey()).iterator(); iterator.hasNext();){
            Vertex vv = iterator.next();
            System.out.print("\t" + vv.get_color());
        }
        System.out.println("");

    }
}

更新 :以下是我用來創建HashMap的代碼。 我以為我正在創建一個HashMap,這樣如果兩個具有相同節點值的頂點對象出現在鍵和值中,它們將共享相同的內存地址並指向相同的頂點對象(即,應該只有一個頂點對象) )。

public Vertex getVertex(Map<Long,Vertex> vertices_map, long node){
        if( ! vertices_map.containsKey(node) ) vertices_map.put(node, new Vertex(node));
        return vertices_map.get(node);
    }

    // read file and return graphs
    public HashMap<Vertex, ArrayList<Vertex>> read_file_and_populate(String file_loc, boolean reverse) throws IOException{

    HashMap<Vertex, ArrayList<Vertex>> graph = new HashMap<Vertex, ArrayList<Vertex>>();
    int l = 0;
    int r = 0;
    if(reverse == false){
        r = 1;
    } else{
        l = 1;
    }

    FileInputStream fil = new FileInputStream(file_loc);
    BufferedReader br = new BufferedReader( new InputStreamReader(fil));
    String element = null;

    while( (element = br.readLine()) != null){
        String[] line = element.split("\\s");
        /*
         *
         *  Create a new vertex object when the node first occurs;
         *  And if the node occurs more than once, return a copy of the
         *  reference to the same object with the same node value.
         *
         */
        HashMap<Long, Vertex> vertices_map = new HashMap<Long, Vertex>();
        Vertex v_l = getVertex(vertices_map , Long.parseLong(line[l]) );
        Vertex v_r = getVertex(vertices_map , Long.parseLong(line[r]) );

        if(graph.containsKey(v_l) == false){
            ArrayList<Vertex> edges_of_this_vertex = new ArrayList<Vertex>();
            edges_of_this_vertex.add(v_r);
            graph.put(v_l, edges_of_this_vertex);
        } else{
            graph.get(v_l).add(v_r);
        }
    }
    return graph;
}

您可以修改的顏色,但是顯示方法只能打印的顏色。 您有一個作為鍵和值的頂點(“ Vertex @ 1f”),這就是為什么在輸出的底行有單獨的“ WHITE”輸出的原因。

更新:

查看這一行(請注意其在代碼流中的位置):

    HashMap<Long, Vertex> vertices_map = new HashMap<Long, Vertex>();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM