Equals and Hashcode in java
Equals and hashcode methods are the two most important methods for java developers to be aware of. equals() and hashcode() methods become very useful when implementing interactions between several classes in large projects. Java provides equals() and hashcode() for every class to test equality and to provide a hash or digest based on the content of the class.
Purpose of equals() and hashCode() method
Java hashCode() and equals() method are used in Hash table-based implementations in java for storing and retrieving data.
The implementation of equals() and hashCode() should follow these rules.
- If o1.equals(o2), then o1.hashCode() == o2.hashCode() should always be true.
- If o1.hashCode() == o2.hashCode is true, it doesn’t mean that o1.equals(o2) will be true.
When to override equals() and hashCode() methods?
Overriding of equals and hashcode is important when you need to use your custom class in java collections as key. When we override equals() method, it’s almost necessary to override the hashCode() method too so that their contract is not violated by our implementation.
read about What is rehashing and load factor?
Implementing equals() and hashCode() method
We can define our own equals() and hashCode() method implementation but if we don’t implement them carefully, it can produce disastrous results. Luckily to our rescue most of the modern IDE these days provide ways to implement them automatically and if needed we can modify them according to our requirement.
Things to remember while overriding hashcode in Java
- When you override equals() method, hashcode() should be overridden to be in compliance with equals and hashcode contract.
- For an immutable object, you can cache the hashcode once generated for improved performance.
- Test your hashcode method for equals hashcode compliance.
- If you don’t override hashCode() method properly your Object may not function correctly on hash-based collection e.g. HashMap, Hashtable or HashSet.
Project Lombok:
Project Lombok is a java library that automatically plugs into your editor and builds tools, spicing up your java.
Never write another getter or equals or hashcode method again, with one annotation your class has a fully-featured builder, Automate your logging variables, and much more.
With the introduction of project Lombok you can easily plug it in into any java project and never have to manually generate getter, setters, constructors, equals and hashcode methods in your POJO classes. Just annotation and it will automatically generate and add these methods into your project.
Normally without Lombok if you modify and Entity or Model or Pojo classes you need to change all getters and setter and constructor and equals and hashcode methods manually and worst is if you forget to change then your code will not behave properly if you are using those classes in Collections. But lombok handles all the pain and handles all the changes automatically.
Lombok Annotations
- @Getter and @Setter: The
@Getter
and@Setter
annotations generate a getter and setter for a field, respectively - @NonNull: The
@NonNull
annotation is used to indicate the need for a fast-fail null check on the corresponding member. - @ToString: This annotation generates an implementation of the
toString
method. - @EqualsAndHashCode: This class level annotation will cause Lombok to generate both
equals
andhashCode
methods, as the two are tied together intrinsically by thehashCode
contract. - @Data: The
@Data
annotation is likely the most frequently used annotation in the Project Lombok toolset. It combines the functionality of@ToString
,@EqualsAndHashCode
,@Getter
, and@Setter
.