This code snippet show you how to use HashCodeBuilder
and EqualsBuilder
class from the Apache Commons Lang library to implement the hashCode()
and equals()
method of an object. To use both of these classes, we just need to create instance of these classes and append the properties that we will use to calculate the hashcode and to test for equality.
Implementing the hashCode()
method first by creating the hashCode()
method. Add the @Override
annotation to make sure that we’ve overridden the correct method. Then we create an instance of HashCodeBuilder
. Append the fields we’re going to use to calculate the hashcode. The final result of the actual hashcode can be obtained by calling the toHashCode()
from the instance of HashCodeBuilder
.
/**
* Implement the hashCode() method using HashCodeBuilder.
*/
@Override
public int hashCode() {
return new HashCodeBuilder().append(id).append(name).toHashCode();
}
We do the same to create the equals()
method. First create the method, it takes a single argument type of java.lang.Object
. Add the @Override
annotation to make sure we override the correct method. On the first line you can check to see if the passed object is an instance of the same object, we use the instanceof
operator. We then compare the values stored in both objects using the EqualsBuilder
class. To get the equality result, we must remember to call the isEquals()
method.
/**
* Implement the equals() method using the EqualsBuilder.
*/
@Override
public boolean equals(Object obj) {
if (!(obj instanceof DummyUser that)) {
return false;
}
return new EqualsBuilder().append(this.id, that.id)
.append(this.name, that.name).isEquals();
}
Here is the complete look of the snippet.
package org.kodejava.commons.lang;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
public class DummyUser {
private Long id;
private String name;
/**
* Constructor to create an instance of this class.
*/
public DummyUser() {
}
public static void main(String[] args) {
DummyUser user1 = new DummyUser();
user1.setId(10L);
user1.setName("Carol");
DummyUser user2 = new DummyUser();
user2.setId(10L);
user2.setName("Carol");
System.out.println("user1.hashCode() = " + user1.hashCode());
System.out.println("user2.hashCode() = " + user2.hashCode());
System.out.println("user1.equals(user2) = " + user1.equals(user2));
}
// Getters & Setters
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
/**
* Implement the hashCode() method using HashCodeBuilder.
*/
@Override
public int hashCode() {
return new HashCodeBuilder().append(id).append(name).toHashCode();
}
/**
* Implement the equals() method using the EqualsBuilder.
*/
@Override
public boolean equals(Object obj) {
if (!(obj instanceof DummyUser that)) {
return false;
}
return new EqualsBuilder().append(this.id, that.id)
.append(this.name, that.name).isEquals();
}
}
The results of our code are:
user1.hashCode() = 64902380
user2.hashCode() = 64902380
user1.equals(user2) = true
Maven Dependencies
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.14.0</version>
</dependency>
- How do I get number of each day for a certain month in Java? - September 8, 2024
- How do I get operating system process information using ProcessHandle? - July 22, 2024
- How do I sum a BigDecimal property of a list of objects using Java Stream API? - July 22, 2024