# @EqualsAndHashCode Lombok annotation

In 
Published 2022-12-03

The project Lombok is a popular Java library that is used to minimize/remove the boilerplate code.

For using Lombok, we need to add the Lombok dependency. My example is using Maven, so the dependency is the following:

<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
   <groupId>org.projectlombok</groupId>
   <artifactId>lombok</artifactId>
   <version>1.18.26</version>
   <scope>provided</scope>
</dependency>

Also, my example is using Spring Boot, and it is created by using Spring Initializr.

The @EqualsAndHashCode annotation is used when we want to let lombok generate implementations of the equals(Object other) and hashCode() methods.

Here are an example of using the @EqualsAndHashCode annotation.

I will create two "Employee" classes and for one class I will use the @EqualsAndHashCode annotation.

Employee.class
package com.example.employee;

import lombok.EqualsAndHashCode;

@EqualsAndHashCode
public class Employee {
    private String id;
    private String name;

    public Employee(String id, String name) {
        this.id = id;
        this.name = name;
    }
}
Employee2.class
package com.example.employee;

public class Employee2 {
    private String id;
    private String name;

    public Employee2(String id, String name) {
        this.id = id;
        this.name = name;
    }
}

And here we can see how @EqualsAndHashCode annotation is working:

SpringApplication.class
package com.example;

import com.example.employee.Employee;
import com.example.employee.Employee2;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringApplication {

	public static void main(String[] args) {
		org.springframework.boot.SpringApplication.run(SpringApplication.class, args);
		System.out.println("Here starts the Spring application !");

		Employee emp1 = new Employee("10", "John");
		Employee emp2 = new Employee("11", "Anna");
		Employee emp3 = new Employee("11", "Anna");

		if (emp2.equals(emp3)) {
			System.out.println("emp2 is equal to emp3");
		} else {
			System.out.println("emp2 is NOT equal to emp3");
		}

		System.out.println("emp1 toString="+emp1.toString());
		System.out.println("emp2 toString="+emp2.toString());
		System.out.println("emp3 toString="+emp3.toString());

		System.out.println("emp1 HashCode="+emp1.hashCode());
		System.out.println("emp2 HashCode="+emp2.hashCode());
		System.out.println("emp3 HashCode="+emp3.hashCode());

		System.out.println("------    Without @EqualsAndHashCode   ------");

		Employee2 emp21 = new Employee2("10", "John");
		Employee2 emp22 = new Employee2("11", "Anna");
		Employee2 emp23 = new Employee2("11", "Anna");

		if (emp22.equals(emp23)) {
			System.out.println("emp22 is equal to emp23");
		} else {
			System.out.println("emp22 is NOT equal to emp23");
		}

		System.out.println("emp21 toString="+emp21.toString());
		System.out.println("emp22 toString="+emp22.toString());
		System.out.println("emp23 toString="+emp23.toString());

		System.out.println("emp21 HashCode="+emp21.hashCode());
		System.out.println("emp22 HashCode="+emp22.hashCode());
		System.out.println("emp23 HashCode="+emp23.hashCode());
	}
}

When we run the code we can see:

Here starts the Spring application !
emp2 is equal to emp3
emp1 toString=com.example.employee.Employee@24c7e9
emp2 toString=com.example.employee.Employee@20adb9
emp3 toString=com.example.employee.Employee@20adb9
emp1 HashCode=2410473
emp2 HashCode=2141625
emp3 HashCode=2141625
------    Without @EqualsAndHashCode   ------
emp22 is NOT equal to emp23
emp21 toString=com.example.employee.Employee2@2e77b8cf
emp22 toString=com.example.employee.Employee2@2c4ca0f9
emp23 toString=com.example.employee.Employee2@67ef029
emp21 HashCode=779598031
emp22 HashCode=743219449
emp23 HashCode=108982313

Process finished with exit code 0