title: Implementation of Hashcode for custom class in Java
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/27581/overriding-equals-and-hashcode-in-java">Overriding equals and hashCode in Java</a>  </p>
  </blockquote>
  
  
  
  <p>I am supposed to implement hashcode and equals for Custom class Person. Person consists of</p>
  
  <blockquote>
    <p>firstname</p>
    
    <p>lastname</p>
  </blockquote>
  
  <p>I am supposed to implement equals and hashcode such that two people with <strong>firstname</strong> and <strong>lastname</strong> should return true for equals and should be accepted by Hashmap. I have implemented Person class like this:</p>
  
  <pre><code>public class Person {
  
  
      String firstname;
      String lastname;
      public Person(String firstname, String lastname) {
          this.firstname = firstname;
          this.lastname = lastname;
      }
  
      @Override
      public int hashCode() {
          // TODO Auto-generated method stub
          return firstname.hashCode()+lastname.hashCode();
      }
  
      @Override
      public boolean equals(Object obj) {
          // TODO Auto-generated method stub
      Person u=(Person) obj;
  
          return u.firstname.equals(firstname) &amp;&amp; u.lastname.equals(lastname);
      }
  
  }
  </code></pre>
  
  <p>Is the implementation of Hashcode correct here? Even though I am getting the expected result,I want to know if this implementation is correct? </p>