current position:Home>[Java interview] how does HashMap resolve hash conflicts?
[Java interview] how does HashMap resolve hash conflicts?
2022-06-24 09:56:23【Learn architecture from mic】
Hi, Hello everyone , I am a Mic.
Today, let's share an interview question about one year's work .
Recently, many fans have encountered this problem during the interview
The problem is :“HashMap Is how to solve Hash Conflicting ”?
Many people think this problem is very simple , But I think the expert's answer will be better .
Let's take a look at the answers of ordinary people and experts to this question .
【Java interview 】Java Work 0 To 3 Annual necessity ,HashMap Is how to solve hash Conflicting ? Amaze the interviewer with expert answers
Need expert interview documents ( A 100000 word interview document inside Alibaba is attached ) Or if you have technical interview questions you don't understand and want to consult, you can send private letters backstage 【Mic】 Or leave a message in the comment area .
Ordinary people :
HashMap solve hash Ways of conflict I remember using the one-way linked list to say if you admit hash Conflict between , It will save the conflicting key value pairs to the end of the linked list .
master :
well , I need to answer this question from several aspects .
First ,HashMap The bottom layer uses an array structure to store data elements , The default length of an array is 16, When we go through put Method to add data ,HashMap according to Key Of hash Value for modulo .
Finally save to the specified location of the array .
But this design will exist hash The question of conflict , That's two different things hash It's worth it key, The final module will fall into the same array subscript .
therefore HashMap Chain addressing method is introduced to solve hash The question of conflict , For conflicting key,HashMap Put these key Form a one-way linked list .
Then use the tail interpolation method to put this key Save to the end of the linked list .
in addition , In order to avoid the problem that the linked list is too long , When the chain length is greater than 8 And the array length is greater than or equal to 64 When ,
HashMap Will transform the linked list into a red black tree .
So as to reduce the time complexity of linked list data query , Improve query performance .
Last , I'll add that , solve hash There are many ways to solve conflict problems , such as
Again hash Law , It's if one of them hash Function conflicts , And another one hash Calculate , For example, bloom filter adopts this method .
Open addressing , It is to directly find an empty array subscript from the conflicting array position for data storage , This is in ThreadLocal It's used to .
Establish a common overflow area , That is to say, the conflicting key Put them in a common overflow area .
The above is my understanding of this problem .
summary
hash The problem of conflict , In the process of business development, we seldom encounter .
But from the solution , Can learn a lot of technical design ideas
Whether for interview or long-term career development , I think this technical point is the basic knowledge that needs to be deeply understood .
Friends who like my works , Remember to like the collection and pay attention
Need expert interview documents ( A 100000 word interview document is attached ) Or if you have technical interview questions you don't understand, you can scan the QR code below
↓↓↓↓↓↓↓↓↓↓↓↓↓↓
copyright notice
author[Learn architecture from mic],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2022/175/202206240833208804.html
The sidebar is recommended
- () is a linear table that restricts the internal structure of data elements to only one character. A. Stack B. queue C. string D. array
- TS cannot find global variable for NPM package
- Java, what should I do if there is an edit configuration in the idea when it runs
- C++ code. Freshmen have just come into contact with this language
- Tnsnames Ora file configuration
- Handling method of Oracle data file header SCN inconsistency
- Oracle database listening file configuration
- Oracle database expdp only exports tables
- Windows method for checking network port occupation and kill process
- Oracle viewing data file header SCN information
guess what you like
Ora-28000 error after upgrading Oracle 12C to 19C
[talk about serviceregistryendpoint of springcloud]
[springcloud service registration and anti registration AOP interception]
[springboot source code analysis - @endpoint annotation validation principle analysis]
[elegant offline and grayscale release of spring cloud]
PostgreSQL
Reactnative 0.69 release
The new design can be called a new generation. The gradient borderless grille has a high appearance value. The application drawing of Changan Ruicheng plus
Linux general command summary
Render external link address URL video page via iframe - Vue
Random recommended
- Detailed explanation of Linux system tuning (VII) -- network status viewing command nethogs
- Vue failed to parse source for import analysis because the content contains invalid JS syntax
- Differences between front-end set and map
- Difference between front-end foreach and map
- Front end array flattening
- How the front end judges the data type
- Front end CSS style expand and collapse
- Front end array de duplication
- Front end throttling and anti shake
- Analysis of 43 cases of MATLAB neural network: Chapter 33 prediction algorithm of fuzzy neural network -- water quality evaluation of Jialing River
- Java & c++ problem solving and expansion -- leetcode30 Concatenate substrings of all words [new knowledge of Mo]
- Eclipse customizes and calls the header file [c/c++]
- Detailed explanation of Vue Foundation
- Unity determines whether all toggles are selected or not
- Program reverse output and print results
- Ubuntu modify time zone
- Linux Installation maven
- Okhttp source code analysis of Android network framework
- Android adds system service interfaces and test cases
- Ubuntu deployment according to Vue version
- Thinkphp5 clear the cache cache, temp cache and log cache under runtime
- Springboot + JWT + redis open source knowledge community system
- Why is JSX syntax so popular?
- The actual test of fluent IOS -- worth collecting
- Front and rear end cross domain issues
- Record the range of data that MySQL update will lock
- Springboot + JWT + redis open source knowledge community system
- 1 minute serverless set up a real website to lead the cat super card: scene experience of "set up nails at a high speed to broadcast the weather regularly"
- 1 minute serverless quickly build a real website to lead the cat super card: scene experience of "rapidly build a zblog blog system"
- Vue eventbus value transfer between brother components
- Array seamless scrolling demo
- Echorts auto play
- Class object oriented programming idea
- elementui utils/clickoutside. JS click element external trigger
- SQL statistics of users logged in for N consecutive days
- Vue3 network request adding loading
- Explain the specific calculation of string hashcode in Java
- Open Oracle server under Linux to allow remote connection
- Properties and simple realization of red black tree
- Vue component introduction and page Jump