How To Use @EqualsAndHashCode Annotation With Project Lombok

@EqualsAndHashCode annotation enables you to avoid having to manually write equals() and hashcode() methods in Java classes.

# Dev Environment:

  • Apache maven (v3.6.1)
  • maven-compiler-plugin (v3.8.1)
  • lombok (v1.18.8)
  • Apache Netbeans (v10.0)

TIP: For those who would like to follow along with this blog, just clone my repository git clone https://github.com/steven7mwesigwa/java-tutorials.git and navigate to java-tutorials\project-lombok\equals-and-hashcode. In there, you should be able to play around with all the source code used in this blog post.

# Terminologies

public boolean equals(Object obj)

equals() method is used to compare if two objects are equal.

public int hashCode()

hashCode() method simply returns a hash code value for an object.

# Demonstration : (Without Lombok)

Don't be scared. For basic use cases, your IDE will implement these methods for you. i.e In Apache Netbeans, Right-click in the editor window where you have your code >> Click Insert Code... (Alt + Insert) in the context menu >> Then select which data members you wish to use for comparision >> Press Generate button.

In our case we chose both fields for comparision of different objects.

Let's create a Person class with equals() and hashcode() implementation. Full code here


//demo1
package com.stevenmwesigwa.equalsandhashcode.demo1;
        
public class Person {

    private String firstname;
    private String lastname;
        
        //Getters
        //Setters
        
    @Override
    public int hashCode() {
        int hash = 7;
        hash = 29 * hash + Objects.hashCode(this.firstname);
        hash = 29 * hash + Objects.hashCode(this.lastname);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Person other = (Person) obj;
        if (!Objects.equals(this.firstname, other.firstname)) {
            return false;
        }
        if (!Objects.equals(this.lastname, other.lastname)) {
            return false;
        }
        return true;
    }
    
}

With lombok, most of this boilerplate code could easily be eliminated by adding a few annotations here and there.

# Demonstration : (With Lombok)

Let's create a Person class and make use of @EqualsAndHashCode annotation. Full code here


//demo2
package com.stevenmwesigwa.equalsandhashcode.demo2;

import lombok.EqualsAndHashCode;
import lombok.Setter;

@EqualsAndHashCode
@Setter
public class Person {

    private String firstname;
    private String lastname;
}

As you can notice, we only needed to add the annotation just above the class definition.This will automatically generate equals() and hashcode() methods based on the fields of your object.

NOTE: Non-transient and non-static fields will be the ones used by default. Though we have the ability to specify which fields are used to generate equals() and hashcode() methods.

That's it for today. Don't hesitate to leave a comment. Your feedback is greatly appreciated.

About The Author   

Steven Mwesigwa

Software Engineer at Vogue Book Consultancy Services Ltd


Picture Of Steven Mwesigwa

Steve is currently a software developer at Vogue Book Consultancy Services Ltd and a technology author. He holds a Dip. in civil engineering from Kyambogo University. He founded and maintains stevenmwesigwa.com a website that receives more than 1.5 thousand visits per month. Steve can be reached on Twitter at @steven7mwwesigwa