我需要比較數百個點才能在2D網格上找到路徑,並且我非常期待性能。 我在Point
的類中重寫equals()
:覆蓋java中的具體類型equals()是否有什麼好處?
@Override
public boolean equals(Object o)
{
if (o instanceof Point)
{
Point that = (Point) o;
return that.i == this.i && that.j == this.j;
}
return false;
}
這是相當不錯的,因爲它可以比較我Point
對象(在ArrayList.contains()
使用),但我經常需要相互之間比較Point
秒。 所以我重載equals()
:
public final boolean equals(Point other)
{
return (i == other.i) && (j == other.j);
}
的問題是:是否有從第二個方法什麼好處?這是更快的情況下,兩個Point
情況比較時,他們直接比較,和的instanceof和投不要求:
boolean result = onePoint.equals(otherPoint);
關於平臺:代碼是在Android上使用的Android SDK(19)編制,它是在iOS上使用avian進行AOT編譯。
非常感謝。
實現comparble並覆蓋compareTo。或者使用比較器,如果你想compareTo&等,http://docs.oracle.com/javase/7/docs/api/java/util/Comparator.html如果你這樣做是正確的,你也可以做Collections.Sort和其他功能如二元搜索等 –