Ho una classe che rappresenta le voci di database con un attributo ID univoco. Is è OK per attuare le equals()
e hashcode()
metodi solo in base a questo attributoQuesto ingenuo è uguale, codice hash OK?
@Override public int hashCode()
{ return id;
}
@Override public boolean equals(Object obj)
{
if (this == obj) return true;
if (obj == null) return false;
if (getClass() != obj.getClass()) return false;
Task other = (Task) obj;
if (id != other.id)
return false;
return true;
}
Non 'HashMap' andare fuori del suo modo di manipolare il codice hash diventa dall'oggetto? – NPE
@NPE [Sì, lo fa.] (Http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/util/HashMap.java#256) – matts