Skip to content

hashCode

Santhosh Kumar Tekuri edited this page Mar 18, 2015 · 1 revision

Let us say you have Employee class defined as below:

public class Employee{
    private String name;
    private String email;
    private int age;
}

We can implement hasCode() as follows:

@Override
public int hashCode(){
    int hashCode = age;
    if(name!=null)
        hashCode += name.hashCode();
    if(email!=null)
        hashCode += email.hashCode();
    return hashCode;
}

i.e, we simply add hashCode of each member and return it;
while doing this, we should be careful to check whether a member is null;

Using Util.hashCode(Object...) you can simplify this as below:

import jlibs.core.lang.Util;

@Override
public int hashCode(){
return age + Util.hashCode(name, email);
}

Here JVM implitly creates an array containing name and email. If you want to avoid extra array creation, you could use Util.hashCode(Object) as below:

import jlibs.core.lang.Util;

@Override
public int hashCode(){
return age + Util.hashCode(name) + Util.hashCode(email);
}

NOTE: Util.hashCode(...) uses java.util.Arrays to compute hashCode for arrays.

Clone this wiki locally