Home Fabio Cortesi Stefan Jäger

Welcome to the Joint-Venture Blog from Fabio Cortesi and Stefan Jäger.

 

 
 

 

How many times in your lifetime have you overwritten the functions equals(), hashCode() or toString()? I did many times…

The Apache Commons Lang Package offers three basic classes for building the equals(), hashCode() or toString() method.

  1. EqualsBuilder
    JavaDoc of the EqualsBuilder
    All available fields can be append to the EqualsBuilder, which checks, if two objects are equals. It works with all primitive datatypes and objects (if they have implemented the equals method, of course).
     
  2. HashCodeBuilder
    JavaDoc of the HashCodeBuilder
    If you overwrite the equals() method, you also have to overwrite the hashCode() method. This builder has almost the same methods like the EqualsBuilder, but you should define two randomly chosen odd numbers in the constructor. Ideally the numbers are unique over all classes in the program.
     
  3. ToStringBuilder
    JavaDoc of the ToStringBuilder
    Appends all the desired members and prints something like Person@7f54[name=Stephen,age=29,smoker=false]

 

 

 

 

 
Leave a comment