Apache Commons Lang3 Builder Example

Maven Dependency:

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.1</version>
</dependency>

Example Code:

package com.mahout.util.test;
 
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
 
public class App {
    public static void main(String[] args) {
 
        Employee oldEmployee = new Employee("Mahmood", "Manager", 30);
 
        Employee employee = new Employee("Mahmood", "Developer", 30);
        System.out.println(employee.toString());
        System.out.println(employee.equals(oldEmployee));
        System.out.println(employee.hashCode());
    }
}
 
class Employee {
 
    private final String name;
    private final String position;
    private final int age;
 
    public Employee(String name, String position, int age) {
        this.name = name;
        this.age = age;
        this.position = position;
    }
 
    @Override
    public boolean equals(Object obj) {
        return EqualsBuilder.reflectionEquals(this, obj);
    }
 
    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE);
    }
 
    @Override
    public int hashCode() {
        return HashCodeBuilder.reflectionHashCode(this);
    }
 
}

Output:

com.mahout.util.test.Employee@112f614[
  name=Mahmood
  position=Developer
  age=30
]
false
-1054694576

Example Code 1:

package com.mahout.util.test;
 
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
 
public class App {
    public static void main(String[] args) {
 
        Employee oldEmployee = new Employee("Mahmood", "Manager", 30);
 
        Employee employee = new Employee("Mahmood", "Developer", 30);
        System.out.println(employee.toString());
        System.out.println(employee.equals(oldEmployee));
        System.out.println(employee.hashCode());
    }
}
 
class Employee {
 
    private final String name;
    private final String position;
    private final int age;
 
    public Employee(String name, String position, int age) {
        this.name = name;
        this.age = age;
        this.position = position;
    }
 
    @Override
    public boolean equals(Object obj) {
        EqualsBuilder builder = new EqualsBuilder();
        return builder.append(this.name, ((Employee) obj).name)
                    .append(this.age, ((Employee) obj).age).isEquals();
    }
 
    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE);
    }
 
    @Override
    public int hashCode() {
        HashCodeBuilder builder = new HashCodeBuilder();
        return builder.append(this.name).append(this.position).toHashCode();
    }
 
}

Output:

com.mahout.util.test.Employee@87816d[
  name=Mahmood
  position=Developer
  age=30
]
true
-260665654
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License