Apache – Simple way for equals() & hashcode()

Simple way to implement equals() & hashcode()

For any POJO classes with multiple attributes, generally equals & hashcode implementation is same in most of the cases. Mostly everyone follow guidelines given by author “Joshua Bloch”. So instead of writing code for equals & hashcode separately in each POJO class, apache provides easy & simple way to implement equals & hashcode methods as shown in below example.

In below example “Person” is a POJO class which needs equals() & hashcode() implementation. So we can use below builder classes provided by apache commons-lang API, which uses all attributes of “person” class to perform equality & to provide hashcode.





You might want to exclude some attribute from equality or hashcode which can also be done very easily using simple annotations.



Leave a Reply

Your email address will not be published. Required fields are marked *