Deep dive into how HashMap works in Java

HashMap basics

--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); 

How HashMap works

(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]

Regarding capacity expansion

--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.

References

https://www.javatpoint.com/working-of-hashmap-in-java

Recommended Posts

Deep dive into how HashMap works in Java
Initializing HashMap in Java
[Java] How Spring DI works
Deep copy collection in Java
How to learn JAVA in 7 days
How to use classes in Java?
How to name variables in Java
How memory works in object-oriented languages
How arrays work in Java (illustration)
How to concatenate strings in java
Study Deep Learning from scratch in Java.
How to implement date calculation in Java
How to implement Kalman filter in Java
Multilingual Locale in Java How to use Locale
First steps for deep learning in Java
[Java] How to use the HashMap class
How to do base conversion in Java
How Dispatcher servlet works in Spring MVC
How to implement coding conventions in Java
How to embed Janus Graph in Java
How to get the date in java
Java Performance Chapter 4 How the JIT Compiler Works
How to display a web page in Java
How to translate Rails into Japanese in general
I tried to implement deep learning in Java
How to get Class from Element in Java
How to hide null fields in response in Java
[Java] Understand in 10 minutes! Associative array and HashMap
[Deep Learning from scratch] in Java 3. Neural network
[Java] How to substitute Model Mapper in Jackson
How to solve an Expression Problem in Java
How Docker works ~ Implement the container in 60 lines
How to write Java String # getBytes in Kotlin?
How much ternary operator is allowed in Java
Partization in Java
Java HashMap class
Changes in Java 11
Rock-paper-scissors in Java
Pi in Java
FizzBuzz in Java
How jul-to-slf4j works
How to call functions in bulk with Java reflection
How to create a Java environment in just 3 seconds
[Java] How to omit the private constructor in Lombok
How to input / output IBM mainframe files in Java?
[Java] How to get the maximum value of HashMap
[Personal memo] Make a simple deep copy in Java
How to create a data URI (base64) in Java
How to generate / verify ID token in Java Memo
How to convert A to a and a to A using AND and OR in Java
How to convert a file to a byte array in Java
How to Git manage Java EE projects in Eclipse
Summary of how to implement default arguments in Java
How to put old Java (8 series) in macOS 10.15 Catalina
Notes on how to use regular expressions in Java