相关疑难解决方法(0)

Java 中浮点数的哈希码

我有一个带有两个浮点变量和hashCode方法的类(当前代码片段中没有 equals):

public class TestPoint2D {
    private float x;
    private float z;

    public TestPoint2D(float x, float z) {
        this.x = x;
        this.z = z;
    }

    @Override
    public int hashCode() {
        int result = (x != +0.0f ? Float.floatToIntBits(x) : 0);
        result = 31 * result + (z != +0.0f ? Float.floatToIntBits(z) : 0);
        return result;
    }
}
Run Code Online (Sandbox Code Playgroud)

下面的测试

@Test
public void tempTest() {
    TestPoint2D p1 = new TestPoint2D(3, -1);
    TestPoint2D p2 = new TestPoint2D(-3, 1);

    System.out.println(p1.hashCode());
    System.out.println(p2.hashCode());
} …
Run Code Online (Sandbox Code Playgroud)

java floating-point hashcode

3
推荐指数
1
解决办法
3145
查看次数

标签 统计

floating-point ×1

hashcode ×1

java ×1