2 Replies Latest reply: Jul 27, 2007 4:32 AM by 800322 RSS

    equals and hashCode

    800435
      Hi,
      I have this class,
      public class ReceitaEstadoVO{ 
          private String regiao;
          private Date data;
          private double valor;
          
          public boolean equals(Object obj){
              boolean result = false;
              if(obj instanceof ReceitaEstadoVO){
                  ReceitaEstadoVO receitaEstado = (ReceitaEstadoVO)obj;
                  if((receitaEstado.getData().compareTo(this.data) == 0) && receitaEstado.getRegiao().equals(this.regiao))
                      result = true;
              }   
              return result;   
          }
          public int 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 this
      if((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