This discussion is archived
1 Reply Latest reply: Feb 26, 2007 2:15 PM by 796440 RSS

How to implement hashCode() whenever an equals() is implemented

807596 Newbie
Currently Being Moderated
public boolean equals(Object obj) {

          // TODO Add the following, if required
          // && format == docDelResponse.getFormat()
          // && viewType == docDelResponse.getViewType() && type ==
          // docDelResponse.getType()
          // && inclCertifiedCopy == docDelResponse.isInclCertifiedCopy()
          // && inclNonPatentLiterature == docDelResponse.isInclNonPatentLiterature()
          // && inclNonUSReferences == docDelResponse.isInclNonUSReferences()
          // && inclUSReferences == docDelResponse.isInclUSReferences()

          if (obj != null && obj instanceof DocumentDeliveryResponse) {
               DocumentDeliveryResponse docDelResponse = (DocumentDeliveryResponse) obj;
               if (identifier != null && identifier.equals(docDelResponse.getIdentifier())) {
                    return true;
               }
          }
          return false;
     }

/**
* @see java.lang.Object#hashCode()
*/
public int hashCode() {
if(identifier != null) {
return identifier.hashCode();
} else {
return super.hashCode();
}
}




they are using the standard
Object.hashCode(). This means that it is highly likely that two "equal" objects
are having different hash codes.

Please let me know how i have to implement hashCode() whenever an equals() is implemented


Thanks