problème d'écrassement avec hashmap
salut
j'ai une hashmap, je store des objets de type Id
Id contient un champ de type long et string
seul un de ces deux champs est utilisé selon l'objet qui l'appèle..
type permet de savoir lequel des deux doit être utilisé... c'est une enum
j'ai redéfinie la méthode toString de cet objet
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
public boolean equals(Object o) {
if (o == this) {
return true;
}
if (o instanceof Id) {
Id other = (Id) o;
if (this.longVal != other.longVal) {
return false;
}
if (this.stringVal != null) {
if (!this.stringVal.equals(other.stringVal)) {
return false;
}
}
if (this.stringVal == null && other.stringVal != null) {
return false;
}
return true;
}
return false;
} |
j'ai tenté de redéfinir la méthode hashCode pour cet objet
Code:
1 2 3 4 5 6 7 8 9 10 11 12
|
public int hashCode() {
int hash = 1;
if (type.equals(Type.longType)) {
hash = new Long(this.longVal).hashCode();
}
if (type.equals(Type.StringType)) {
hash = new Long(this.stringVal).hashCode();
}
return hash;
} |
j'insère 6 valeur et je commence à avoir des écrassements de valeurs
une idée pour éviter ce problème?
merci