Sunday, May 15, 2011

Implementing hashCode using commons-lang

This example shows how to implement hashCode() using HashCodeBuilder of commons-lang.

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


public class MyClass implements Serializable {
private Long id;

private String title;

private String author;

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

// return HashCodeBuilder.reflectionHashCode(this);



Also, reflection can be used to implement the same function:

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

No comments:

Post a Comment