Click here to Skip to main content
Click here to Skip to main content

Apache Commons EqualsBuilder and HashCodeBuilder

, 6 Jan 2011
Rate this:
Please Sign up or sign in to vote.
Apache Commons EqualsBuilder and HashCodeBuilder

Introduction

Before we go ahead and explore Apache Commons EqualsBuilder and HashCodeBuilder, we must know the relationship between equals and hashCode.

java.lang.Object which is the super class of all Java classes has two very important methods defined in it. They are:

  • public boolean equals(Object obj)
  • public int hashCode()

These two methods are very important when our classes deal with other Java classes such as Collection API used for searching, sorting, comparison and eliminate duplicate objects from a set.

public boolean equals(Object obj)

This method checks if some other object passed to it as an argument is equal to the object on which this method is invoked. The default implementation of this method in Object class simply checks if two object references x and y refer to the same object, i.e., it checks if x == y. This particular comparison is also known as “shallow comparison”. However, the classes providing their own implementations of the equals method are supposed to perform a “deep comparison”.

public int hashCode()

This method returns the hash code value for the object on which this method is invoked. This method returns the hash code value as an integer and is supported for the benefit of hashing based collection classes such as Hashtable, HashMap, HashSet, etc.

Relationship between equals and hashCode

Equal objects must produce the same hash code as long as they are equal, however unequal objects need not produce distinct hash codes.

Implementation

While hashCode() and equals() typically impact logic and performance, they are also often more tricky to implement correctly. The most important rule is that when one of these two methods is overridden, the other one should be as well. Because it can be tricky to implement hashCode() and equals() correctly, it is helpful to have EqualsBuilder and HashCodeBuilder two reusable implementations of these provided as part of Apache Commons Lang builder package.

I particularly like the way how EqualsBuilder uses reflection to determine if the two Objects are equal or HashCodeBuilder uses reflection to build a valid hash code. But reflection hits the performance of application, so use it where the performance is not very critical. However these two builders also provide the alternative approach. Let’s demonstrate both approaches one by one:

  • Using Reflection:

    Let’s create a simple class called Employee as the following mentioned code:

    package blog.commons;
    
    import org.apache.commons.lang.builder.EqualsBuilder;
    import org.apache.commons.lang.builder.HashCodeBuilder;
    
    public class Employee {
    
    	private int id;
    	private String name;
    	private int deptId;
    	private String designation;
    
    	public Employee(int id, String name, int deptId, String designation) {
    		this.id = id;
    		this.name = name;
    		this.deptId = deptId;
    		this.designation = designation;
    	}
    
    	@Override
    	public boolean equals(Object obj) {
    		return EqualsBuilder.reflectionEquals(this, obj);
    	}
    
    	@Override
    	public int hashCode() {
    		return HashCodeBuilder.reflectionHashCode(this);
    	}
    }

    Now create a Main class to check how it works:

    package blog.commons;
    
    import java.util.Arrays;
    import java.util.HashSet;
    import java.util.Set;
    
    public class Main {
    
    	public static void main(String[] args) {
    		Employee emp1 = new Employee(1, "Allen", 12, "Accountant");
    		Employee emp2 = new Employee(1, "Allen", 13, "Accountant");
    		System.out.println
    			("Is emp1 equals to emp2: " + emp1.equals(emp2));
    		Set<Employee> employees = new HashSet<Employee>();
    		employees.addAll(Arrays.asList(emp1,emp2));
    
    		System.out.println("Size of set: " + employees.size());
    
    	}
    }

    If you execute the main method, the output will be as follows:

    Is emp1 equals to emp2: false
    Size of set: 2

    Here you can see that the employee “Allen” whose employee Id is “1? is an “Accountant” does his book keeping job for two different departments which department ids are 12 and 13 respectively. But the object emp1 and emp2 are treated as two different persons.

    Now, let’s exclude the deptId field to determine equality and build hashCode and see the effect. For doing this, you need to slightly modify the code of Employee class:

    package blog.commons;
    
    import org.apache.commons.lang.builder.EqualsBuilder;
    import org.apache.commons.lang.builder.HashCodeBuilder;
    
    public class Employee {
    
    	private int id;
    	private String name;
    	private int deptId;
    	private String designation;
    
    	public Employee(int id, String name, int deptId, String designation) {
    		this.id = id;
    		this.name = name;
    		this.deptId = deptId;
    		this.designation = designation;
    	}
    
    	@Override
    	public boolean equals(Object obj) {
    		return EqualsBuilder.reflectionEquals
    			(this, obj,new String[] {"deptId"});
    	}
    
    	@Override
    	public int hashCode() {
    		return HashCodeBuilder.reflectionHashCode
    				(this,new String[] {"deptId"});
    	}
    }

    Again, execute the same main method to see the output:

    Is emp1 equals to emp2: true
    Size of set: 1

    Here, as you can see, now emp1 and emp2 are treated as equal but emp1 are been replaced by emp2, hence the size of the set employees is one. To overcome this problem, do not exclude the deptId while generating hashCode. Your hashCode method should look like the following:

    @Override
    	public int hashCode() {
    		return HashCodeBuilder.reflectionHashCode(this);
    	}

    Now the output should be shown as below if you execute the main method again:

    Is emp1 equals to emp2: true
    Size of set: 2
  • Without Using Reflection:

    In my previous examples, you have seen how easily and in a fascinating manner we can use reflection capability of EqualsBuilder and HashCodeBuilder to override equals and hashCode methods. Now, I am going to show you how can you achieve the same output without using reflection which helps to improve performance:

    package blog.commons;
    
    import org.apache.commons.lang.builder.EqualsBuilder;
    import org.apache.commons.lang.builder.HashCodeBuilder;
    
    public class Employee {
    
    	private int id;
    	private String name;
    	private int deptId;
    	private String designation;
    
    	public Employee(int id, String name, int deptId, String designation) {
    		this.id = id;
    		this.name = name;
    		this.deptId = deptId;
    		this.designation = designation;
    	}
    
    	@Override
    	public boolean equals(Object obj) {
    		if (obj instanceof Employee == false) {
    			return false;
    		}
    
    		if (this == obj) {
    			return true;
    		}
    		Employee other = (Employee) obj;
    		return new EqualsBuilder().append(this.id , other.id)
    		.append(this.name , other.name)
    		.append(this.designation , other.designation).isEquals();
    	}
    
    	@Override
    	public int hashCode() {
    		return new HashCodeBuilder().append(this.id)
    		.append(this.name)
    		.append(this.deptId)
    		.append(this.designation)
    		.hashCode();
    	}
    }

    If you run the main method this time, you get similar output as it has been shown in the last output result. Just notice here that I am not appending deptId in EqualsBuilder but I am doing it while generating hashCode.

Like these EqualsBuilder and HashCodeBuilder Apache Commons project provides many such reusable APIs to speed up your application development time. You can find more information regarding Apache Commons from the following link:


License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Bikash Shaw
Software Developer (Senior) Société Générale
India India
Summary:
 
1. Extensive working experience in web and windows application development, database programming using Java and .Net technologies.
 
2. Good knowledge in SDLC and Processes like project planning, requirement gathering, development, test planning, release management and production support.
 
3. Good knowledge and working experience in following methodologies:
a. Agile Development Approach
b. Test Driven Development
c. Behavior Driven Development
d. Continuous Integration & Delivery Model.
 
4. Excellent communication & analytical skills, good team player, great mentoring capability.
 
5. Interaction with customer and team spread over different geographical area.
 
Technologies / Languages: J2SE 5/6, J2EE, .Net 2.0/3.5, C#, ASP.NET, AJAX, XHTML, CSS, JavaScript, jQuery, PL/SQL, Web Services (SOAP based), Winforms, Hibernate, Spring, GWT, XML, XSD, XPath, SAX, JAXB, JUnit, JBehave, Mockito, Selenium, StringTemplate, Log4J, Apache Commons API
 
Database: Oracle, SQL Server, MySQL, Sybase
 
Application Server: Tomcat, Sun Application Server, JBoss, GlassFish, Jetty, IIS
 
Development Environments: Eclipse, NetBeans, Visual Studio.
 
Designing Tools: Enterprise Architect, Microsoft Visio
 
Version Control: SVN, Perforce
 
Build Management: Hudson & JCruisemonitor, TeamCity
 
Bug Tracking Tools: HP Quality Center, Bugzilla, JIRA
 
Specialties:
1. Agile Test and Behavior Driven Development
2. Continuous Delivery Approach
2. Spring IOC and MVC
3. Web Services
Follow on   Twitter

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 6 Jan 2011
Article Copyright 2011 by Bikash Shaw
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid