我想創建一個搜索算法,它將座標對存儲在名爲HashSquareSpec的包裝類中。爲了避免重複,並保持插入順序,我將每個HashSquareSpec插入LinkedHashSet。儘管我已經重寫了equals()方法和hashCode()方法,但LinkedHashSet仍然接受兩個HashSquareSpec對象,其中有相同的座標對,其中包含 。LinkedHashSet不刪除重複
public static void main(String [] args)
{
LinkedHashSet<HashSquareSpec> firedShots = new HashLinkedSet<HashSquareSpec>();
HashSquareSpec a = new HashSquareSpec(1,2);
HashSquareSpec b = new HashSquareSpec(2,2);
HashSquareSpec c = new HashSquareSpec(1,2);
HashSquareSpec d = new HashSquareSpec(3,2);
firedShots.add(a);
firedShots.add(b);
firedShots.add(c);
firedShots.add(d);
System.out.println(a.equals((SquareSpec)c));
Iterator l = firedShots.iterator();
while(l.hasNext())
{
System.out.println(l.next().hashCode());
}
}
Output:
true
38444474
38474265
38444474
38504056
HashSquare類
public class HashSquareSpec extends SquareSpec
{
public HashSquareSpec(int sx, int sy)
{
super(sx,sy);
}
public HashSquareSpec(String codeString)
{
super(codeString);
}
@Override
public int hashCode()
{
return this.toString().hashCode();
}
public boolean equals(HashSquareSpec other)
{
if(this.toString().equals(other.toString()))
return true;
else
return false;
}
}
和超級類HashSquareSpec
public class SquareSpec {
public int x;
public int y;
public SquareSpec(int sx, int sy) {
this.x = sx;
this.y = sy;
}
public SquareSpec(String codeString) {
this.x = Integer.parseInt(codeString.substring(1,2));
this.y = Integer.parseInt(codeString.substring(3,4));
}
public String toString() {
return("(" + x + "," + y + ")");
}
public boolean equals(SquareSpec other) {
return (other.x == this.x &&
other.y == this.y);
}
}
的儘管有許多不同的hashCode變化和Eclipse等於和hashCode一代, firedShots數據結構保持接受重複。我的代碼有什麼問題?
這不是一個很好的'equals'實現;你應該做'instanceof'檢查。 (例如,你的實現不是對稱的,因爲這將等於一個String。) –
@LouisWasserman非常真實,這就是爲什麼包括一個關於空值檢查的句子,並檢查'other'是否是相同類型的。 – rgettman