HashMap supports an array, then why is it disordered

I read that the HashMap has a support array where records are stored (marked with a bucket number, initial size 16). Arrays are ordered, and I can call get (n) to get the element at the nth position. Then why the HashMap is unordered and does not have a get (n) method?

+4
source share
2 answers

It depends on your idea of ​​what orderly means.

It is true HashMapsinternally to use an array or another set that has a fixed order . However, the order has nothing for the insertion order or something like that. Elements are ordered, for example, by increasing the size of the hash values , and they have nothing to do with the actual ordering of the elements themselves.

So it HashMapreally has something like a method get(n)if you think that nis the hash value of the key element. The method is called get(*key*), and it first calculates the hash-value of the given key element, and then looks at the value in the internal structure using get(*hash-value*)it.

, , HashSet s:

HashSet Structure

, HashSet HashMaps, . , , , .


. - - , , , hash-value, . , , -, .

, HashMap, , - 1 2 .., . , , HashMap .

+4

A HashMap . , , , , -. , .

, , : HashMap Java 8

+2

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


All Articles