【发布时间】:2011-09-13 00:13:50
【问题描述】:
我有自己的类,它是一个对象,其中包含一组其他对象。 即
public class Curve{
@Override
public Collection<CurvePoint> getCurvePoints() {
return curvePoints;
}
@Override
public boolean equals(Object other) {
if (other instanceof Curve) {
Curve otherCurve = (Curve) other;
return getCurvePoints().equals(otherCurve.getCurvePoints());
}
return false;
}
}
CurvePoint 类实现 Comparable 并且还覆盖了 equals 方法,如下所示:
public class CurvePoint implements ICurvePoint, Comparable<CurvePoint> {
@Override
public int compareTo(CurvePointother) {
return getSnapDate().compareTo(other.getSnapDate());
}
@Override
public boolean equals(Object other) {
if (other instanceof CurvePoint) {
CurvePointotherPoint = (CurvePoint) other;
return (getId().equals(otherPoint.getId())
&& getBid().getRate() == otherPoint.getBid().getRate()
&& getOffer().getRate() == otherPoint.getOffer().getRate() && getMid()
.getRate() == otherPoint.getMid().getRate());
}
return false;
}
}
我的问题是,当我有 2 个 Curve 集合时,如何比较它们以检查它们是否相等?当我使用 .equals 时,它总是只返回 false,有没有办法在不遍历两个集合的情况下做到这一点?
【问题讨论】:
-
不。标准集合的 equals() 实现总是比较所有子集合。其他一切都会很糟糕
标签: java collections equals