S
S
silentproger2017-04-29 23:08:48
Java
silentproger, 2017-04-29 23:08:48

Why does a HashMap use a hashcode and not just links?

Actually, the question is, why was this mechanism invented with hashcodes, baskets, in which there are lists of objects with the same hashcodes? Why is there no simple link->link map?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey〒., 2017-04-29
@silentproger

The hashcode is used to check the uniqueness of the stored key objects. But since the uniqueness of the hash is limited by the length of the int type, a collision may occur for this, and linked lists of objects with the same hashcodes are used.
Why not just links? Imagine that we sequentially add two equivalent key objects to the Map, their references are different. It turns out that there will be two objects in the Map, they have the same hashes, but different references. And, by definition, in a Map, all keys must be unique.

M
Maxim Moseychuk, 2017-04-29
@fshp

Equivalent objects must have equivalent identifiers.
Links cannot provide this.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question