简体   繁体   中英

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 . 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(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). assuming the int is the only state in Vertex, the hashCode & equals functions should be based on those eg

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. The add() function to a arraylist returns a boolean (always true). So, when you do a graph.get(v_l), you will always get a boolean true.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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