Beispiel

public EqualsHashCode_Example(String s){
super();
this .s = s;
}

public int hashCode(){
return s.hashCode();
}

private String s = ""; //$NON-NLS-1$

Lösung
Implementieren Sie sowohl die Methode 'hashCode()' als auch die Methode 'equals()'.

public EqualsHashCode_Solution(String s){
super();
this .s = s;
}

public int hashCode(){
return s.hashCode();
}

public boolean equals( Object other ) {
return ( other instanceof EqualsHashCode_Solution ) &&
((EqualsHashCode_Solution)other).s.equals( s );
}

private String s = ""; //$NON-NLS-1$