覆盖自定义哈希码和等于 class
Overriding hashcode and equals for custom class
我创建了一个包含 2 个字段的 class。 Double 和 Line2D。我想覆盖 equals 方法,以便下面的代码 return true
public class Main {
public static void main(String[] args) {
StatusLinePair slp1 = new StatusLinePair(25.0, new Line2D.Double(123.0, 32.0, 342.0, 54.0));
StatusLinePair slp2 = new StatusLinePair(25.0, new Line2D.Double(123.0, 32.0, 342.0, 54.0));
System.out.println(slp1.equals(slp2));
}
}
这是我尝试过的方法,但我仍然没有得到想要的结果
public class StatusLinePair {
public Double yAxisOrder;
public Line2D line;
public StatusLinePair(Double yAxisOrder, Line2D line) {
this.yAxisOrder = yAxisOrder;
this.line = line;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((line == null) ? 0 : line.hashCode());
result = prime * result + ((yAxisOrder == null) ? 0 : yAxisOrder.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
StatusLinePair other = (StatusLinePair) obj;
if (this.yAxisOrder == other.yAxisOrder && this.line.getX1() == other.line.getX1()
&& this.line.getX2() == other.line.getX2() && this.line.getY1() == other.line.getY1()
&& this.line.getY2() == other.line.getY2())
return true;
return false;
}
}
请帮帮我。提前致谢!
您没有使用 Line2D.equals,所以我假设这没有按照您需要的方式实现,否则您会使用它。如果是这种情况,您也不应该使用 Line2D.hashCode()。
简而言之,要么使用Line2D.hashCode()和Line2D.equals(),要么不使用,不要混合使用。
您的代码有几个问题:
- 如果另一个对象不是 StatusLinePair,它应该 return false,而不是抛出 ClassCastException
- 如果另一个对象为 null,它应该 return false,而不是抛出 NullPointerException
- 它不应该将
Double
实例与 == 进行比较,而是与 equals()
进行比较(或者 class 应该包含双精度而不是双精度,因为它似乎不可为空):这就是您的特定问题的原因。
- 它不应该使用
Line2D.hashCode()
,因为它不使用 Line2D.equals()
通过使用 ==,您不是在比较对象的实际值,而是在比较引用。
For example:
Object a = new Object();
Object b = a; // if you compare these two objects with == it will return true
But
Object a =new Object();
Object b = new Object(); // if you compare these two objects with == then it will return false as they are pointing to two different objects
通过覆盖 equals(),您可以根据两个对象的值比较它们。 Check this link out for more on equals.
看这里:
重载 Equals() 和运算符 == 的指南:
https://msdn.microsoft.com/en-us/library/ms173147(v=vs.80).aspx
和
重载 GetHashCode 的指南:
https://msdn.microsoft.com/en-us/library/system.object.gethashcode(v=vs.80).aspx
它在 C# 中,但几乎相同!
我创建了一个包含 2 个字段的 class。 Double 和 Line2D。我想覆盖 equals 方法,以便下面的代码 return true
public class Main {
public static void main(String[] args) {
StatusLinePair slp1 = new StatusLinePair(25.0, new Line2D.Double(123.0, 32.0, 342.0, 54.0));
StatusLinePair slp2 = new StatusLinePair(25.0, new Line2D.Double(123.0, 32.0, 342.0, 54.0));
System.out.println(slp1.equals(slp2));
}
}
这是我尝试过的方法,但我仍然没有得到想要的结果
public class StatusLinePair {
public Double yAxisOrder;
public Line2D line;
public StatusLinePair(Double yAxisOrder, Line2D line) {
this.yAxisOrder = yAxisOrder;
this.line = line;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((line == null) ? 0 : line.hashCode());
result = prime * result + ((yAxisOrder == null) ? 0 : yAxisOrder.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
StatusLinePair other = (StatusLinePair) obj;
if (this.yAxisOrder == other.yAxisOrder && this.line.getX1() == other.line.getX1()
&& this.line.getX2() == other.line.getX2() && this.line.getY1() == other.line.getY1()
&& this.line.getY2() == other.line.getY2())
return true;
return false;
}
}
请帮帮我。提前致谢!
您没有使用 Line2D.equals,所以我假设这没有按照您需要的方式实现,否则您会使用它。如果是这种情况,您也不应该使用 Line2D.hashCode()。
简而言之,要么使用Line2D.hashCode()和Line2D.equals(),要么不使用,不要混合使用。
您的代码有几个问题:
- 如果另一个对象不是 StatusLinePair,它应该 return false,而不是抛出 ClassCastException
- 如果另一个对象为 null,它应该 return false,而不是抛出 NullPointerException
- 它不应该将
Double
实例与 == 进行比较,而是与equals()
进行比较(或者 class 应该包含双精度而不是双精度,因为它似乎不可为空):这就是您的特定问题的原因。 - 它不应该使用
Line2D.hashCode()
,因为它不使用Line2D.equals()
通过使用 ==,您不是在比较对象的实际值,而是在比较引用。
For example:
Object a = new Object();
Object b = a; // if you compare these two objects with == it will return true
But
Object a =new Object();
Object b = new Object(); // if you compare these two objects with == then it will return false as they are pointing to two different objects
通过覆盖 equals(),您可以根据两个对象的值比较它们。 Check this link out for more on equals.
看这里:
重载 Equals() 和运算符 == 的指南: https://msdn.microsoft.com/en-us/library/ms173147(v=vs.80).aspx
和
重载 GetHashCode 的指南: https://msdn.microsoft.com/en-us/library/system.object.gethashcode(v=vs.80).aspx
它在 C# 中,但几乎相同!