我有自己的类,其中包含一组其他对象。 即。
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,有没有办法在没有遍历两个集合的情况下完成它?
答案 0 :(得分:0)
如果您要比较集合,那么集合需要具有类似的界面,这对于集合来说是正确的。
基本上可以,但是如果当前集合没有按照您喜欢的方式实现比较,则需要对该集合进行子类化并覆盖方法compareTo(...)
和equals(...)
。
答案 1 :(得分:0)
由于你没有显示CurvePoint的整个代码只是一个检查: getBid()。getRate()等同样返回Primitives,而不是Wrapper对象,对吧?因为你将它们与==而不是equals()进行比较。
答案 2 :(得分:0)
检查以下链接,您将找到答案:
simple-way-to-find-if-two-different-lists-contain-exactly-the-same-elements
is-there-a-way-to-check-if-two-collections-contain-the-same-elements-independent
答案中提供了许多有用的详细信息,因此请检查所有内容,而不仅仅是接受的答案。