title: Java - HashMap.containsKey returning incorrect values for Objects
body: <p>Here is my code:</p>
  
  <pre><code>import java.util.HashMap;
  
  class MyString
  {
    String string;
  
    MyString(String string)
    {
      this.string = new String(string);
    }
  }
  
  public class test
  {
  
    public void test3()
    {
      HashMap&lt;MyString, Byte&gt; byteHashMap = new HashMap&lt;MyString, Byte&gt;();
      char c = 'A';
      byte b = (byte)c;
  
      String string = new String("The Letter A");
      MyString myString = new MyString(string);
  
      byteHashMap.put(myString, b);
  
      if (byteHashMap.containsKey(myString))
      //if (byteHashMap.containsKey(new MyString(string)))
      {
        System.out.println("true");
      }
      else
      {
        System.out.println("false");
      }
    }
  
    public static void main(String[] args)
    {
      test testObject = new test();
      testObject.test3();
    }
  }
  </code></pre>
  
  <p>I would like to know why the code, as is, returns true. However, when I switch the if statements (see the commented line), it returns false.</p>
  
  <p>I believe it has something to do with overriding the equals method in the MyString class but I am unsure how to approach this. </p>
  
  <p>Regards.</p>