and EqualsAndHashcode
Lombok is something I use nearly every day now that I am developing in Java.
Project Lombok is a java library that automatically plugs into your editor and build tools, spicing up your java.
https://projectlombok.org/

Put simply, Lombok adds lots of annotations that make it easier to write simple methods. For example, when you have a variable, you normally have to write a Getter method to retrieve it, and a Setter method to change it.
String variable
// Getter
public String getVariable(){
return variable
}
// Setter
public void setVariable(String newVariable){
this.variable = newVariable
}
These methods are simple, however it soon becomes annoying have to write them for every single variable, and takes up lots of space in the code base. Using Lombok’s @Getter and @Setter annotations solves this issue. By simply adding the annotation at the top of your class, all variables will have Getter and Setter methods, without you having to actually write them. Lombok also has @Data which will apply both, as well as some other annotations, further reducing the amount of code necessary.
Another useful annotation is @EqualsAndHashcode which generates equals() and hashcode() methods for the class. You can also pass an argument into it to call the super class for the methods for any class that implements another.
What are Equals and Hashcode:
Before I started working on my current team I considered equals to be good enough for comparing equality between two objects. That was until I saw @EqualsAndHashcode.
Here is the method definition for equals:
equals(Object obj): a method provided by java.lang.Object that indicates whether some other object passed as an argument is “equal to” the current instance. The default implementation provided by the JDK is based on memory location — two objects are equal if and only if they are stored in the same memory address.
https://dzone.com/articles/working-with-hashcode-and-equals-in-java
And here is hash code:
hashcode(): a method provided by java.lang.Object that returns an integer representation of the object memory address. By default, this method returns a random integer that is unique for each instance. This integer might change between several executions of the application and won’t stay the same.
https://dzone.com/articles/working-with-hashcode-and-equals-in-java
equals() is checking that two objects are the same instance in the same memory address, and hashcode() generates a random code for each instance. For two objects to be truly equal, they should be stored in the same memory address, with the same hash code.
This is where @EqualsAndHashcode comes in. By using this annotation, you can override the equals() and hashcode() methods, to define what you want to base the equality check on. This is useful in a business environment where two objects might need to be classed as equal based on certain attributes, even if they are stored in different places. For example:
Object(int id, String name)
Object obj1 = new Object(1, "Object")
Object obj2 = new Object(1, "Object")
Using the normal equals() and hashcode() methods to compare these two objects would result in false as they are two different instances.
obj1.equals(obj2) // false
obj1.hashcode() // 039422
obj2.hashcode() // 231453
However for our business purposes, as the two have the same id, we want them to be equal. So if we use the annotation @EqualsAndHashCode(of="id") we can overwrite our methods to use id as the comparator. It is important we overwrite both equals() and hashcode() so that we can work with Hash data structures such as HashMap, HashSet and HashTable, where duplicates are not allowed and hashcode is used to determine equality. Overwriting them basically changes the methods to look like this (although this is likely highly simplified):
// equals
public boolean equals(Object object){
return this.getId() == object.getId()
}
// hashcode
public int hashcode(){
return id
}
Now our two objects are classed as equal.
obj1.equals(obj2) // true
obj1.hashcode() // 1
obj2.hashcode() // 1
Theres lots of useful annotations in Lombok, and also lots of other libraries that add even more helpful annotations, but Lombok is the most common one I have seen and used so far.
You can find the list of all of my Knowledge Sharing posts here.
