title: Hash Map Object Key
body: <p>I'm using this class as my key to Hashmap with overriden hasCode() and equals()</p>
  
  <pre><code>public class Design {
  private double[] factors = null;
  
  public double[] getFactors() {
      return factors;
  }
  
  public void setFactors(double[] factors) {
      this.factors = factors;
  }
  
  public Design(double[] factors) {
      this.factors = factors;
  }
  
  @Override
  public int hashCode() {
      final int prime = 31;
      int result = 1;
      result = prime * result + Arrays.hashCode(factors);
      return result;
  }
  
  @Override
  public boolean equals(Object obj) {
      if (this == obj)
          return true;
      if (obj == null)
          return false;
      if (!(obj instanceof Design))
          return false;
      Design other = (Design) obj;
      if (!Arrays.equals(factors, other.factors))
          return false;
      return true;
  }
  </code></pre>
  
  <p>I added values to map using a loop</p>
  
  <pre><code> public static void main(String[] args) {
  
      Map&lt;Design, Double&gt; map = new HashMap&lt;Design, Double&gt;();
  
      double[] dbl = new double[2];
  
      for(int i=0; i&lt;5; i++){
          for(int j=0; j&lt;2; j++){
              System.out.println(j+i);
              dbl[j] = j+i;
          }
          Design des = new Design(dbl);
          map.put(des, Double.valueOf(i));
      }
  
      for(Design d: map.keySet()){
          System.out.println(d.getFactors()[0] + "\t" + d.getFactors()[1]);
      }
  
      for(double d: map.values()){
          System.out.println(d);
      }
  }
  </code></pre>
  
  <p>The problem is in the key values. It displayed the last key added.</p>
  
  <pre><code>4.0 5.0
  4.0 5.0
  4.0 5.0
  4.0 5.0
  4.0 5.0
  </code></pre>
  
  <p>Where am I getting wrong?</p>