简体   繁体   English

Java:如何检查哈希图的键包含对象?

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

Basically, I am trying to read in a text file using the following code in order to build a graph using the adjacency lists representation. 基本上,我试图使用以下代码读取文本文件,以便使用邻接列表表示形式构建图形。 However, I ran into 2 problems. 但是,我遇到了两个问题。

The first and the major problem is that: I do not understand when I check graph.contains(v_l) , it always return false . 第一个也是主要的问题是:当我检查graph.contains(v_l)时,我不明白,它总是返回false I have been stuck in this problem for ages now. 我已经被这个问题困扰了很久了。 Really need some help here. 真的需要一些帮助。

The second problem: I do not understand why is that within the if statement, I cannot do the following: 第二个问题:我不明白为什么在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);
 }

I have no idea why this is happening ? 我不知道为什么会这样?

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

Below is some example data : 以下是一些示例数据:

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 

Your value class (Vertex) needs to implement hashCode & equals(), otherwise all the operations will be done by checking to see if its the same instance (which it'll never be in this case). 您的值类(Vertex)需要实现hashCode&equals(),否则所有操作都将通过检查其是否为同一实例(在这种情况下永远不会实现)来完成。 assuming the int is the only state in Vertex, the hashCode & equals functions should be based on those eg 假设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;
}

In

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

you are trying to add the result of edges_of_this_vertex.add(v_r) to the graph. 您正在尝试将edge_of_this_vertex.add(v_r)的结果添加到图形中。 The add() function to a arraylist returns a boolean (always true). 数组列表的add()函数返回一个布尔值(始终为true)。 So, when you do a graph.get(v_l), you will always get a boolean true. 因此,当您执行graph.get(v_l)时,将始终获得布尔值true。

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

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