title: Java Hashmap get value from object key
body: <p>Beginner question: I have a hashmap which stores an array of integers as values. The key for each value is an object which consists of two integers (coordinate).</p>
  
  <p>My question: how can I retrieve a value from the hashmap, based on the two coördinates within my object (my 'key')?</p>
  
  <p>My Coords Class (with a little help from Eclipse):</p>
  
  <pre><code>    public class Coords {
      int x;
      int y;
  
      public Coords(int x, int y) {
          super();
          this.x = x;
          this.y = y;
      }
  
      @Override
      public int hashCode() {
          final int prime = 31;
          int result = 1;
          result = prime * result + x;
          result = prime * result + y;
          return result;
      }
  
      @Override
      public boolean equals(Object obj) {
          if (this == obj)
              return true;
          if (obj == null)
              return false;
          if (getClass() != obj.getClass())
              return false;
          Coords other = (Coords) obj;
          if (x != other.x)
              return false;
          if (y != other.y)
              return false;
          return true;
      }
  
  }
  </code></pre>
  
  <p>Building the Hashmap:</p>
  
  <pre><code>public class BuildMap {
  
  public Coords coords;
  public int[] someData = new int[4];
  public Random random = new Random();
  HashMap&lt;Coords, int[]&gt; map = new HashMap&lt;Coords, int[]&gt;();
  
  public void buildHashMap() {
  
      // coordinates from (0,0) to (31,31)
      for (int i = 0; i &lt; 32; i++) {
          for (int j = 0; j &lt; 32; j++) {
  
              coords = new Coords(i, j);
  
              // Every Coord gets a few random numbers
              for (int k = 0; k &lt; 4; k++) {
                  someData[k] = random.nextInt(8564);
              }
  
              map.put(coords, someData);
  
          }
      }
  </code></pre>
  
  <p>If I want to access the array on coordinates 12,13, how can I retrieve it? Is iteration needed (I hope not, I want to add 100,000+ coordinates and quick access ofcourse).</p>
  
  <p>I was hoping this would work somewhat in the line of </p>
  
  <pre><code>int[] theValues = map.get(new Coords(12,13));
  </code></pre>
  
  <p>I hope you can help me. thanks in advance!</p>