Posted in java, Software Engineering

How to override equals and hashCode and What is the effective way to do it?

Before we begin, let’s read something from java Documentation

equals() (javadoc) must define an equality relation (it must be reflexive, symmetric, and transitive). In addition, it must be consistent (if the objects are not modified, then it must keep returning the same value). Furthermore, o.equals(null) must always return false.

hashCode() (javadoc) must also be consistent (if the object is not modified in terms of equals(), it must keep returning the same value).

The relation between the two methods is:

Whenever a.equals(b), then a.hashCode() must be same as b.hashCode().
In practice:

If you override one, then you should override the other.

Use the same set of fields that you use to compute equals() to compute hashCode().

Luckily there are the excellent helper classes EqualsBuilder and HashCodeBuilder from the Apache Commons Lang library. An example:

public class Employee {

private String name;

private int age;

// …

public int hashCode() {

return new HashCodeBuilder(17, 31). // two randomly chosen prime numbers

// if deriving: appendSuper(super.hashCode()).

append(name).

append(age).

toHashCode();

}

public boolean equals(Object obj) {

if (!(obj instanceof Employee))

return false;

if (obj == this)

return true;

Employee rhs = (Employee) obj;

return new EqualsBuilder().

// if deriving: appendSuper(super.equals(obj)).

append(name, rhs.name).

append(age, rhs.age).

isEquals();

}

}

Also remember:

When using a hash-based Collection or Map such as HashSet, LinkedHashSet, HashMap, Hashtable, or WeakHashMap, make sure that the hashCode() of the key objects that you put into the collection never changes while the object is in the collection. The bulletproof way to ensure this is to make your keys immutable, which has also other benefits.

Posted in Software Engineering

How to sort a List of Bean in java

Let say we have a List object declared as the following:

class Bean {

private String name;

private Integer identityNo;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public Integer getIdentityNo() {

return identityNo;

}

public void setIdentityNo(Integer identityNo) {

this.identityNo = identityNo;

}

}

and then in the main class we have:

List<Bean> listBean = new ArrayList<Bean>();

How do we sort listBean ?

There are 2 ways to sort listBean using a standard Java library:

1. Implements Comparable in the class Bean and then call Collections.sort :

class Bean implements Comparable<Bean> {

@Override

public int compareTo(Bean otherObject) {

return getIdentityNo().compareTo(otherObject.getIdentityNo());

}

}

Collections.sort(listBean);

2. Using Collection only:

Collections.sort(listBean, new Comparator<Bean>() {

@Override

public int compare(Bean b1, Bean b2)

{

return b1.getIdentityNo().compareTo(b2.getIdentityNo());

}

});

Personally I would prefer to use the 2nd method.