Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java自身对象和.equals的集合_Java_Collections_Equals - Fatal编程技术网

Java自身对象和.equals的集合

Java自身对象和.equals的集合,java,collections,equals,Java,Collections,Equals,我有自己的类,它是一个对象,里面有一组其他对象。 i、 e 公共类曲线{ @凌驾 公共集合getCurvePoints(){ 返回曲线点; } @凌驾 公共布尔等于(对象其他){ if(曲线的其他实例){ 曲线其他曲线=(曲线)其他; 返回getCurvePoints().equals(otherCurve.getCurvePoints()); } 返回false; } } 其中CurvePoint类实现了Compariable,也覆盖了equals方法,如下所示: publi

我有自己的类,它是一个对象,里面有一组其他对象。 i、 e

公共类曲线{
@凌驾
公共集合getCurvePoints(){
返回曲线点;
}
@凌驾
公共布尔等于(对象其他){
if(曲线的其他实例){
曲线其他曲线=(曲线)其他;
返回getCurvePoints().equals(otherCurve.getCurvePoints());
}
返回false;
}        
}
其中CurvePoint类实现了Compariable,也覆盖了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;
    }

}
公共类CurvePoint实现ICurvePoint,可比较{
@凌驾
公共整数比较(曲线输入其他){
返回getSnapDate().compareTo(other.getSnapDate());
}
@凌驾
公共布尔等于(对象其他){
if(曲线点的其他实例){
CurvePointotherPoint=(CurvePoint)其他;
返回(getId().equals(otherPoint.getId())
&&getBid().getRate()==otherPoint.getBid().getRate()
&&getOffer().getRate()==otherPoint.getOffer().getRate()&&getMid()
.getRate()==otherPoint.getMid().getRate());
}
返回false;
}
}

我的问题是,当我有两个曲线集合时,如何比较它们以检查它们是否相等?当我使用.equals时,它总是返回false,有没有一种方法可以在不循环两个集合的情况下执行此操作?

如果要比较集合,则集合需要有一个可比较的接口,该接口对集合执行正确的操作


基本上这是可能的,但是如果当前集合没有以您喜欢的方式实现比较,那么您需要对该集合进行子类化,并重写方法
compareTo(…)
equals(…)

,因为您没有显示CurvePoint的整个代码,只需检查一下:getBid().getRate()同样的,返回原语,而不是包装对象,对吗?因为您将它们与==而不是equals()进行比较。

否。标准集合的equals()实现始终比较所有子级。其他所有内容都可能与我在curvePoint中重写了compareTo()方法的结果完全相同,因为我希望它们按照曲线中排序集的日期进行排序。我认为集合上的.equals所做的是循环并调用.compareTo,我希望它使用.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;
    }

}