How do I implement hashCode() method using HashCodeBuilder class?

package org.kodejava.commons.lang;

public class ObjectHashCodeDemo {

    public static void main(String[] args) {
        Book book1 = new Book(1L, "Spring Boot in Action", "Craig Walls");
        Book book2 = new Book(2L, "Docker in Action", "Jeff Nickoloff");
        Book book3 = book1;

        System.out.println("book1.hashCode() = " + book1.hashCode());
        System.out.println("book2.hashCode() = " + book2.hashCode());
        System.out.println("book3.hashCode() = " + book3.hashCode());
    }
}
package org.kodejava.commons.lang;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;

import java.io.Serializable;

public class Book implements Serializable {
    private Long id;
    private String title;
    private String author;

    public Book(Long id, String title, String author) {
        this.id = id;
        this.title = title;
        this.author = author;
    }

    //~ Implements getters and setters here.

    public boolean equals(Object o) {
        if (o == this) {
            return true;
        }

        if (!(o instanceof Book)) {
            return false;
        }

        Book that = (Book) o;
        return new EqualsBuilder()
            .append(this.id, that.id)
            .append(this.title, that.title)
            .append(this.author, that.author)
            .isEquals();

        // You can also use reflection of the EqualsBuilder class.
        // return EqualsBuilder.reflectionEquals(this, that);
    }

    public int hashCode() {
        return new HashCodeBuilder()
            .append(id)
            .append(title)
            .append(author)
            .toHashCode();

        // Or even use the simplest method using reflection below.
        // return HashCodeBuilder.reflectionHashCode(this);
    }
}

Maven Dependencies

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

Maven Central

How do I convert an array of primitives into an array of objects?

To convert from primitive arrays into object type arrays, we can use the Apache Commons Lang library. The Commons Lang provides an ArrayUtils class that does this conversion. To convert the other way just use the toPrimitive() method.

package org.kodejava.commons.lang;

import org.apache.commons.lang3.ArrayUtils;

public class ArrayPrimitiveObjectConversionDemo {
    public static void main(String[] args) {
        int[] numbers = {1, 2, 3, 4, 5};
        boolean[] booleans = {true, false, false, true};
        float[] decimals = {10.1f, 3.14f, 2.17f};

        Integer[] numbersObjects = ArrayUtils.toObject(numbers);
        Boolean[] booleansObjects = ArrayUtils.toObject(booleans);
        Float[] decimalsObjects = ArrayUtils.toObject(decimals);

        numbers = ArrayUtils.toPrimitive(numbersObjects);
        booleans = ArrayUtils.toPrimitive(booleansObjects);
        decimals = ArrayUtils.toPrimitive(decimalsObjects);
    }
}

Maven Dependencies

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

Maven Central

How do I use Apache Commons Lang ToStringBuilder class?

The toString() method defined in the java.lang.Object can be overridden when we want to give more meaningful information about our object. We can simply return any information of the object in the toString() method, for instance the value of object’s states or fields.

The Apache Commons Lang library offers a good utility for creating this toString() information. Here I give a simple example using the ToStringBuilder class.

package org.kodejava.commons.lang;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

public class ToStringBuilderDemo {
    private Long id;
    private String firstName;
    private String lastName;

    public static void main(String[] args) {
        ToStringBuilderDemo demo = new ToStringBuilderDemo();
        demo.id = 1L;
        demo.firstName = "First Name";
        demo.lastName = "Last Name";

        System.out.println(demo);
    }

    public String toString() {
        return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE)
            .append("id", id)
            .append("firstName", firstName)
            .append("lastName", lastName)
            .toString();
    }
}

The ToStringStyle class allows us to choose the styling for our toString() method when we print it out. Here are the available styles that we can use.

  • ToStringStyle.DEFAULT_STYLE
  • ToStringStyle.JSON_STYLE
  • ToStringStyle.MULTI_LINE_STYLE
  • ToStringStyle.NO_CLASS_NAME_STYLE
  • ToStringStyle.NO_FIELD_NAMES_STYLE
  • ToStringStyle.SHORT_PREFIX_STYLE
  • ToStringStyle.SIMPLE_STYLE

The result of the code above is:

org.kodejava.commons.lang.ToStringBuilderDemo@8efb846[
  id=1
  firstName=First Name
  lastName=Last Name
]

Below are example results of the other ToStringStyle:

  • ToStringStyle.DEFAULT_STYLE
org.kodejava.commons.lang.ToStringBuilderDemo@d716361[id=1,firstName=First Name,lastName=Last Name]
  • ToStringStyle.JSON_STYLE
{"id":1,"firstName":"First Name","lastName":"Last Name"}
  • ToStringStyle.NO_CLASS_NAME_STYLE
[id=1,firstName=First Name,lastName=Last Name]
  • ToStringStyle.NO_FIELD_NAMES_STYLE
org.kodejava.commons.lang.ToStringBuilderDemo@d716361[1,First Name,Last Name]
  • ToStringStyle.SHORT_PREFIX_STYLE
ToStringBuilderDemo[id=1,firstName=First Name,lastName=Last Name]
  • ToStringStyle.SIMPLE_STYLE
1,First Name,Last Name

If you want to make the code event more simple by using the ToStringBuilder.reflectionToString() method to generate the string for the toString() method to return. Using this method the ToStringBuilder will the hard job of finding information about our class and return the string information.

Maven Dependencies

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

Maven Central