title: Java: How to check hashmap's keys contain object?
body: <p>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. </p>
  
  <p>The first and the major problem is that: I do not understand when I check <code>graph.contains(v_l)</code>, it always return <code>false</code>. I have been stuck in this problem for ages now. Really need some help here.</p>
  
  <p>The second problem: I do not understand why is that within the <code>if</code> statement, I cannot do the following:</p>
  
  <pre><code>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&lt;Vertex&gt; edges_of_this_vertex = new ArrayList&lt;Vertex&gt;();
                  edges_of_this_vertex.add(v_r);
                  graph.put(v_l, edges_of_this_vertex);
   }
  </code></pre>
  
  <p>I have no idea why this is happening ? </p>
  
  <pre><code> 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&lt;Vertex, ArrayList&lt;Vertex&gt;&gt; read_file_and_populate(String file_loc, boolean reverse) throws IOException{
  
          HashMap&lt;Vertex, ArrayList&lt;Vertex&gt;&gt; graph = new HashMap&lt;Vertex, ArrayList&lt;Vertex&gt;&gt;();
          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&lt;Vertex&gt; edges_of_this_vertex = new ArrayList&lt;Vertex&gt;();
                  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;
      }
  }
  </code></pre>
  
  <p>Below is some example data :</p>
  
  <pre><code>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 
  </code></pre>