About Object.hashcode () and Collisions

I read the JavaDoc for the method Object.hashCode, it says that

As reasonably practical, the hashCode method defined by the Object class returns different integers for different objects. (Usually this is done by converting the internal address of the object to an integer [...])

But whatever its implementation, the method hashCodealways returns an integer (let it be assumed positive), so the given Integer.MAX+1different objects, two of them will have the same hash code.

Why is the JavaDoc "denied" collision here? This is a practical conclusion, given that the internal address is used and "come on, you will never have objects at Integer.MAX+1once, so we can say that it is almost always unique"?

EDIT

This bug entry (thanks to Sleiman Jneidi ) gives an exact idea (I think this is more than a 10 year discussion):

it seems that many, perhaps most programmers, think that the default implementation and therefore System.identityHashCode will create unique hash codes.

The β€œHow reasonable is practical” qualification is not enough in practice to clearly show that hash codes are not different in practice.

+4
3

, bug, , , , , JVM , , , , , 32-

+3

-:

http://eclipsesource.com/blogs/2012/09/04/the-3-things-you-should-know-about-hashcode/

, , : " Integer.MAX + 1 , , " - - .

, , -, 77163 , 50/50 -.

0

, , , ( " " ), , - .

, , - . , - .

For example, ConcurrentHashMap , which will use a (executable function) hash code to assign a location to an object on the map. In practice, a hash code is used to find where the object is roughly located, and peers are used to find exactly located.

A hash map cannot use this optimization unless objects try to maximize their hash codes.

0
source

Source: https://habr.com/ru/post/1620540/


All Articles