java examples
Home Java Examples Resources Java Interview Questions
Brainpower SEO

How to implement hashCode using commons-lang?

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

import java.io.Serializable;

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

    //~ Impelments geter/setter here

    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);

    }    
}