equals and hashCode

Hi,

I have this class,

publicclass ReceitaEstadoVO{

private String regiao;

private Date data;

privatedouble valor;

publicboolean equals(Object obj){

boolean result =false;

if(objinstanceof ReceitaEstadoVO){

ReceitaEstadoVO receitaEstado = (ReceitaEstadoVO)obj;

if((receitaEstado.getData().compareTo(this.data) == 0) && receitaEstado.getRegiao().equals(this.regiao))

result =true;

}

return result;

}

publicint hashCode(){

int result = 17;

result = 37*result + regiao.hashCode();

result = 37*result + data.hashCode();

return result;

}

}

so, two objects are equals if they have the same Date and region. My doubt is if thisif((receitaEstado.getData().compareTo(this.data) == 0)

is a correct way to compare Dates and

result = 37*result + data.hashCode();

is the correct way to incorporate the date in the hashCode?

Thanks in advance,

Manuel Leiria

[1899 byte] By [manuel.leiriaa] at [2007-11-27 11:54:35]
# 1

Use equals instead of receitaEstado.getData().compareTo(this.data) == 0)

Kaj

kajbja at 2007-7-29 18:56:54 > top of Java-index,Java Essentials,Java Programming...
# 2

hashCode is fine. Note that there's usually no point in providing excessive "uniqueness" unless you're going to handle lots of different instances.

CeciNEstPasUnProgrammeura at 2007-7-29 18:56:54 > top of Java-index,Java Essentials,Java Programming...