--Probably the most used one in the implementation class of Map (key, value stored data format). --Data can be searched at high speed using the Hash function (it is said that the amount of calculation is less than the linear search of arrays and lists).
HashMap<String, Integer> map = new HashMap<>();
map.put("Sato", 22);
map.put("Takahashi", 24);
map.put("Nomura", 32);
(1) Consider an example where the number of HashMaps that can be stored is 5.
(2) Attempts to store `` `key: Sato value: 22. Calculate the hash value 144 of Sato (it is an appropriate number because it is an example) b. Divide 144 by 5 of the number of elements to get the remainder 144/5 = 28 ... 4 c. Store
key: Sato value: 22``` in 4.
[0]
[1] [Nomura, 32]
[2]
[3] [Takahashi, 24]
[4] [Sato,22]
(3) When retrieving data, perform the same calculation to calculate where the target data is stored from the key. (4) Excess Hash values may collide, and multiple data may be stored in the same location (example below).
[0]
[1] [Nomura, 32]
[2]
[3] [Takahashi, 24]
[4] [Sato,22] [Ito, 98]
--When the number of elements increases, rehash is performed to increase the number of stored items. In the case of Java, it is calculated based on the load coefficient. --Rehash is performed when the number of elements exceeds `` `capacity × load coefficient ```. --Java's HashMap has a default capacity of 16 and a load factor of 0.75.
https://www.javatpoint.com/working-of-hashmap-in-java
Recommended Posts