如何在java 1.6中实现Objects.hashcode

时间:2013-05-30 17:30:46

标签: java java-7 java-6

我有一些代码:

@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();
    }
}

我的平等解决方案如下:

@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;
}

3 个答案:

答案 0 :(得分:4)

JDK 1.6中没有任何内容,但自己编写它是完全无足轻重的:

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

或者 - 最好是,IMO - 开始使用Guava,它有一个熟悉的Objects类。 (它不完全一样,因为它只有版本通过varargs获取数组,但这只意味着你可以在一次调用中编写你的方法。)

我怀疑你会发现,如果你仔细观察Guava,那里有很多有用的东西你会开始使用 - 我知道我不想写任何大量的Java代码这些没有番石榴的日子或类似的东西。

如果这是[{1}}的依赖 ,我自己实现该方法,但我相信还有其他一些你会觉得有用的东西。

答案 1 :(得分:2)

这是一个选择:

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

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

com.google.common.base.Objects

答案 2 :(得分:0)

等于方法就是问题所在。一个新的Double(0.0)== new Double(0.0)计算结果为false。