Java - HashCode & Equals

HashCode & Equals :

  • Every object have access equals method.
  • package : java.lang.object
  • Default implementation of equals, just simply compares memory address of the object.
  • If you override equals you must override hashcode as well. Otherwise it is the violation of general contract for Object-hashcode will occur.
  • Hashcode common algorithm is to start with some arbitrary number and repeatedly multiply with another
  • For Example,
int prime = 31;
int result = 1;
result = prime * result + ( ( firstname==null ) ? 0 : firstName.hashcode());
result = prime * result + ( ( lastName == null ) ? 0 : lastName.hashcode());
return result;

Comments

Popular posts from this blog

How to set Java Object into JaxBElement ?

GitLab