There is a class Point (float x, float y) and a HashMap for storing them. But it is necessary that the points, both coordinates of which differ by less than 10 ^ -4, are considered to be one key, therefore the standard hash generation method will not work.

How to set such a method correctly?

  • And what prevents you from first removing duplicates, and then collecting a HashMap? - rjhdby
  • one
    Do you need a hash code calculation or do you have a problem with points that you need to solve? - avp

1 answer 1

The only mathematically appropriate method is to assign the same hashcode to all numbers.

The fact is that from any number you can “step up” to any other with steps of 10 ^ -4 (or less), so that your requirement gives rise to the requirement that the hashcodes are the same at each step (we exclude the case of very large numbers, in which step 10 ^ -4 is not possible due to loss of accuracy).

Note that the same hashcode case is the worst for a hash table. This means that you have selected an inappropriate data structure for your purposes.

  • Why not just round the coordinates of the points? Those. we have a grid with a step of 10 ^ -4 and we simply calculate which square the point fell into. - avp
  • @avp: This contradicts the condition that the hashcodes of any two points at a distance <ε should be the same. You can take two points on different sides of the grid node for an example. - VladD
  • Formally, yes. And for practical implementation may be suitable. Of course, instead of one reference to the table, you will need up to 9 (we look at the calculated square and its neighbors) with viewing all points in each (again, it depends on the task that we don’t know) - avp
  • @avp: The problem with this implementation will be that it will work on simple input data. And in some cases, it will silently mysteriously give the wrong result. I suspect that TS'u needs clustering in reality, but this is a complex topic. - VladD
  • If you stupidly look for rounded coordinates, then yes. If we use them to narrow the search space, and conduct the search itself in accordance with the physical meaning of a specific task , then I do not see any problems. - avp