簡體   English   中英

Java:如何檢查哈希圖的鍵包含對象?

[英]Java: How to check hashmap's keys contain object?

基本上,我試圖使用以下代碼讀取文本文件,以便使用鄰接列表表示形式構建圖形。 但是,我遇到了兩個問題。

第一個也是主要的問題是:當我檢查graph.contains(v_l)時,我不明白,它總是返回false 我已經被這個問題困擾了很久了。 真的需要一些幫助。

第二個問題:我不明白為什么在if語句中無法執行以下操作:

if(graph.containsKey(v_l) == false){
                // this always fail               
                graph.put(v_l, edges_of_this_vertex.add(v_r));
                // the following works though 
                ArrayList<Vertex> edges_of_this_vertex = new ArrayList<Vertex>();
                edges_of_this_vertex.add(v_r);
                graph.put(v_l, edges_of_this_vertex);
 }

我不知道為什么會這樣?

 class Vertex{
            int node;
            ....
            public Vertex(int node){
                 this.node = node;
            }

            public String toString(){
                 return Integer.toString(node);
            }
    }
class dgraph{
 // constructor ...

 // instance method
 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");
            Vertex v_l = new Vertex( Integer.parseInt(line[l]) );
            Vertex v_r = new Vertex( Integer.parseInt(line[r]) );
            System.out.println("l = " + l + " r = " + 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);
                //graph.put(v_l, edges_of_this_vertex.add(v_r));
            } else{
                graph.get(v_l).add(v_r);

            }
        }
        return graph;
    }
}

以下是一些示例數據:

1 1 
1 2 
1 5 
1 6 
1 7 
1 3 
1 8 
1 4 
2 47646 
2 47647 
2 13019 
2 47648 
2 47649 
2 47650 
2 7700 
2 47651 
2 47652 
3 511596 
5 1 
5 9 

您的值類(Vertex)需要實現hashCode&equals(),否則所有操作都將通過檢查其是否為同一實例(在這種情況下永遠不會實現)來完成。 假設int是頂點中的唯一狀態,則hashCode&equals函數應基於例如

public int hashCode() {
   return node *31;
}

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

// this always fail               
            graph.put(v_l, edges_of_this_vertex.add(v_r));

您正在嘗試將edge_of_this_vertex.add(v_r)的結果添加到圖形中。 數組列表的add()函數返回一個布爾值(始終為true)。 因此,當您執行graph.get(v_l)時,將始終獲得布爾值true。

暫無
暫無

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

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