1

我有一些这样的代码:

@Override
public int hashCode() {
    int hash = 5;
    hash = 47 * hash + Objects.hashCode(this.bendWidth);
    hash = 47 * hash + Objects.hashCode(this.bendSideLength);
    hash = 47 * hash + Objects.hashCode(this.thickness);
    hash = 47 * hash + Objects.hashCode(this.innerRadius);
    hash = 47 * hash + Objects.hashCode(this.bendAngle);
    hash = 47 * hash + Objects.hashCode(this.kfactor);
    hash = 47 * hash + Objects.hashCode(this.bendShortening);
    return hash;
}

我希望让这个源代码适用于 1.6

到目前为止,我已经尝试过番石榴:

@Override
public int hashCode() {
    int hash = 5;


    Object[] objs = new Object[]{
        this.getPointND().getPoint()[0],
        this.getPointND().getPoint()[1],
        this.getPointND().getPoint()[2],
        this.getPointND().getPoint()[3],
        this.getPointND().getPoint()[4],
        this.kfactor,
        this.bendShortening
    };

    hash = 47 * hash + Objects.hashCode(objs);

    hash = 47 * hash + Objects.hashCode(this.bendWidth);
    hash = 47 * hash + Objects.hashCode(this.bendSideLength);
    hash = 47 * hash + Objects.hashCode(this.thickness);
    hash = 47 * hash + Objects.hashCode(this.innerRadius);
    hash = 47 * hash + Objects.hashCode(this.bendAngle);
    hash = 47 * hash + Objects.hashCode(this.kfactor);
    hash = 47 * hash + Objects.hashCode(this.bendShortening);
    return hash;
}

我已经尝试过这样的解决方案:

   @Override
    public int hashCode() {
        int hash = 5;


        Object[] objs = new Object[]{
            this.getPointND().getPoint()[0],
            this.getPointND().getPoint()[1],
            this.getPointND().getPoint()[2],
            this.getPointND().getPoint()[3],
            this.getPointND().getPoint()[4],
            this.kfactor,
            this.bendShortening
        };

        hash = 47 * hash + Objects.hashCode(objs);

        hash = 47 * hash + Objects.hashCode(this.bendWidth);
        hash = 47 * hash + Objects.hashCode(this.bendSideLength);
        hash = 47 * hash + Objects.hashCode(this.thickness);
        hash = 47 * hash + Objects.hashCode(this.innerRadius);
        hash = 47 * hash + Objects.hashCode(this.bendAngle);
        hash = 47 * hash + Objects.hashCode(this.kfactor);
        hash = 47 * hash + Objects.hashCode(this.bendShortening);
        return hash;
    }

但是这个测试仍然失败:

@Test
public void testHashCodeIsDifferentHashCode() {
    try {
        DataPoint pointOne = new DataPoint();
        pointOne.setBendAngle(new Double(1));
        pointOne.setBendShortening(new Double(1));
        pointOne.setBendSideLength(1);
        pointOne.setBendWidth(1);
        pointOne.setInnerRadius(1);
        pointOne.setKfactor(new Double(1));
        pointOne.setThickness(1);

        DataPoint pointTwo = new DataPoint();
        pointTwo.setBendAngle(0);
        pointTwo.setBendShortening(new Double(0));
        pointTwo.setBendSideLength(0);
        pointTwo.setBendWidth(0);
        pointTwo.setInnerRadius(0);
        pointTwo.setKfactor(new Double(0));
        pointTwo.setThickness(0);

        DataPoint pointThree = new DataPoint();
        pointThree.setBendAngle(Double.NaN);
        pointThree.setBendShortening(Double.NaN);
        pointThree.setBendSideLength(Double.NaN);
        pointThree.setBendWidth(Double.NaN);
        pointThree.setInnerRadius(Double.NaN);
        pointThree.setKfactor(Double.NaN);
        pointThree.setThickness(Double.NaN);

        Set<DataPoint> map = new HashSet<DataPoint>();
        map.add(pointOne);
        map.add(pointTwo);
        assert (map.size() == 2);
    } catch (NullPointerException ex) {
        assert false : "failed due to null";
    } catch (Exception ex) {
        assert false : "failed, unknown error.";
    }
}

试过这个:

@Override
public int hashCode() {
    int hash = 5;
    hash = 47 * hash + Objects.hashCode(this.bendWidth);
    hash = 47 * hash + Objects.hashCode(this.bendSideLength);
    hash = 47 * hash + Objects.hashCode(this.thickness);
    hash = 47 * hash + Objects.hashCode(this.innerRadius);
    hash = 47 * hash + Objects.hashCode(this.bendAngle);
    hash = 47 * hash + Objects.hashCode(this.kfactor);
    hash = 47 * hash + Objects.hashCode(this.bendShortening);
    return hash;
}
public class Objects {

    public static int hashCode(Object object) {
        return object == null ? 0 : object.hashCode();
    }
}

我的equals解决方案是这样的:

@Override
public boolean equals(Object obj) {
    if (obj instanceof DataPoint) {
        DataPoint od = (DataPoint) obj;
        return (this.bendAngle == od.bendAngle)
                && (this.bendShortening == od.bendShortening)
                && (this.bendSideLength == od.bendSideLength)
                && (this.bendWidth == od.bendWidth)
                && (this.innerRadius == od.innerRadius)
                && (this.kfactor == od.kfactor)
                && (this.thickness == od.thickness);
    }
    return false;
}
4

3 回答 3

4

JDK 1.6 中没有任何内容,但自己编写它完全是微不足道的:

public static int hashCode(Object object) {
    return object == null ? 0 : object.hashCode();
}

或者 - 最好是 IMO - 开始使用具有熟悉的类的GuavaObjects。(这并不完全相同,因为它只有版本通过可变参数获取数组,但这只是意味着您可以在一次调用中编写您的方法。)

我怀疑你会发现,如果你仔细观察 Guava,里面有很多有用的东西你会开始使用——我知道这些天如果没有 Guava,我不想写任何大量的 Java 代码或非常相似的东西。

如果这只是一个依赖项Objects.hashCode会自己实现该方法,但我敢肯定还有其他有用的东西。

于 2013-05-30T17:33:33.960 回答
2

这是一种选择:

@Override
public int hashCode() {
    return Arrays.asList(this.bendWidth, this.x, this.y).hashCode();
}

是 null 安全的,并生成良好的哈希码。quava 中还有一个 Objects 类:

com.google.common.base.Objects
于 2013-05-30T17:33:19.643 回答
0

equals 方法是问题所在。a new Double(0.0) == new Double(0.0) 计算结果为假。

于 2013-06-03T17:52:39.340 回答