範例

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

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

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

解決方案
實作 hashCode() 和 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$