Hash Codes for Floats in Java

隐身守侯 提交于 2019-12-07 18:08:35

问题


I have a class with two float variables and hashCode method (without equals in current code snippet):

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

The following test

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

returns same values:

-2025848832

In this case I can't use my TestPoint2D within HashSet / HashMap

Can anyone suggest how to implement hashCode in this case or workarounds related to this?

P.S. Added one more test:

@Test
public void hashCodeTest() {
    for (float a = 5; a < 100000; a += 1.5f) {
        float b = a + 1000 / a; // negative value depends on a
        TestPoint3D p1 = new TestPoint3D(a, -b);
        TestPoint3D p2 = new TestPoint3D(-a, b);
        Assert.assertEquals(p1.hashCode(), p2.hashCode());
    }
}

And it is passed that proves that

TestPoint2D(a, -b).hashCode() == TestPoint2D(-a, b).hashCode()


回答1:


These auto-generated hashcode functions are not very good.

The problem is that small integers cause very "sparse" and similar bitcodes.

To understand the problem, look at the actual computation.

System.out.format("%x\n", Float.floatToIntBits(1));
System.out.format("%x\n", Float.floatToIntBits(-1));
System.out.format("%x\n", Float.floatToIntBits(3));
System.out.format("%x\n", Float.floatToIntBits(-3));

gives:

3f800000
bf800000
40400000
c0400000

As you can see, the - is the most significant bit in IEEE floats. Multiplication with 31 changes them not substantially:

b0800000
30800000
c7c00000
47c00000

The problem are all the 0s at the end. They get preserved by integer multiplication with any prime (because they are base-2 0s, not base-10!).

So IMHO, the best strategy is to employ bit shifts, e.g.:

final int h1 = Float.floatToIntBits(x);
final int h2 = Float.floatToIntBits(z);
return h1 ^ ((h2 >>> 16) | (h2 << 16));

But you may want to look at Which hashing algorithm is best for uniqueness and speed? and test for your particular case of integers-as-float.




回答2:


I would use Objects.hash():

public int hashCode() {
   return Objects.hash(x, z);
}

From the Javadoc:

public static int hash(Object... values)

Generates a hash code for a sequence of input values. The hash code is generated as if all the input values were placed into an array, and that array were hashed by calling Arrays.hashCode(Object[]). This method is useful for implementing Object.hashCode() on objects containing multiple fields. For example, if an object that has three fields, x, y, and z, one could write:




回答3:


according to the java specification, 2 objects can have the same hashCode and this doesnt mean they are equal...

the probability is small but exist...

on the other hand is always a good practice to override both equals and hashcode...




回答4:


As I understand the problem, you expect a lot of symmetrical pairs of points among your keys, so you need a hashCode method that does not tend to give them the same code.

I did some tests, and deliberately giving extra significance to the sign of x tends to map symmetrical points away from each other. See this test program:

public class Test {
  private float x;
  private float y;

  public static void main(String[] args) {
    int collisions = 0;
    for (int ix = 0; ix < 100; ix++) {
      for (int iz = 0; iz < 100; iz++) {
        Test t1 = new Test(ix, -iz);
        Test t2 = new Test(-ix, iz);
        if (t1.hashCode() == t2.hashCode()) {
          collisions++;
        }
      }
    }
    System.out.println(collisions);

  }

  public Test(float x, float y) {
    super();
    this.x = x;
    this.y = y;
  }

  @Override
  public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = (x >= 0) ? 1 : -1;
    result = prime * result + Float.floatToIntBits(x);
    result = prime * result + Float.floatToIntBits(y);
    return result;
  }
  // Equals omitted for compactness
}

Without the result = (x >= 0) ? 1 : -1; line it is the hashCode() generated by Eclipse, and counts 9802 symmetrical point collisions. With that line, it counts one symmetrical point collision.



来源:https://stackoverflow.com/questions/36848151/hash-codes-for-floats-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!