当前位置:网站首页>Hashcode details
Hashcode details
2022-07-25 02:53:00 【young_ man2】
Today, when reviewing the hash table of data structure, I saw HashCode The concept , Then I don't understand it so much , Just record it !
Then I refer to the explanation of the boss , With some understanding , The original link is as follows :
hashcode Detailed explanation - Lao Wang with dreams - Blog Garden (cnblogs.com)
1. What is? HashCode as well as hashcode How did you get it ?
1.1 What is? hash?

1.2 What is? hashcode?
hashcode It is obtained in a certain way hash The corresponding position in the table .
1.3 hashcode How did you get it ?
First of all, an object must have a physical address , In other blog posts hashcode Said to represent the address of the object , This will certainly make readers form misunderstandings , The physical address of the object is the same as this hashcode Different addresses ,hashcode The address representing the object means that the object is in hash The position in the table , The physical address is the address where the object is stored in memory , So how do objects get hashcode Well ? Through the internal address of the object ( That's the physical address ) Convert to an integer , Then the integer passes through hash The algorithm of the function gets hashcode, therefore ,hashcode What is it? ? Is in the hash The corresponding position in the table . If it's not clear here , for instance ,hash Table has hashcode by 1、hashcode by 2、(...)3、4、5、6、7、8 These eight positions , There is an object A,A Convert the physical address of to an integer 17( This is if ), Through the direct remainder algorithm ,17%8=1, that A Of hashcode for 1, And A It's just hash In the table 1 The location of . There must be other questions , Then look at the following , Here is just an example to let you know what is hashcode The meaning of .
2. hashcode What's the role ?
HashCode The main purpose of the existence of is to find the shortcut ,HashCode It is used to determine the storage address of the object in the hash storage structure
Why? hashcode Just find it faster , such as : We have one that can store 1000 In such a large number of memory , In which to store 1000 A different number , In the dumbest way , Just save a number , Just go through it , See if there is the same number , When saved 900 A digital , Start saving 901 A number , Just follow 900 Compare two numbers , It's a lot of trouble , It's time consuming , use hashcode To record the location of the object , Take a look at .hash Table has 1、2、3、4、5、6、7、8 A place , Save the first number ,hashcode by 1, The number is placed in hash In the table 1 The location of , Deposit in 100 A digital ,hash In the table 8 There will be many numbers in one position ,1 There may be 20 A digital , save 101 When a digital , He checked first hashcode The position corresponding to the value , Assuming that 1, Then there is 20 A number and his hashcode identical , He just needs to talk to this 20 Compare two numbers (equals), If each one is the same , Then put it in 1 This position , In this way, the number of comparisons is much less , actually hash There are many places in the table , Here is just an example 8 individual , So the number of comparisons will make you feel a lot , actually , If hash The watch is big , Then the number of comparisons is very few . Through the original method and use hashcode Method comparison , We'll see hashcode The role of , And why use hashcode 了
3. equals and hashcode The relationship between
Through the previous example , You can probably know , Through the first hashcode To compare , If hashcode equal , Then use equals Method to compare the equality of two objects , Use an example to illustrate : Above said hash In the table 8 A place , like 8 A barrel , Each bucket can hold a lot of objects , object A adopt hash The function algorithm gets it and puts it in 1 In No.1 barrel , Of course, there must be other objects that will be put in 1 In No.1 barrel , If the object B It is also divided into 1 Bucket number , So how does it identify whether other objects in the bucket are the same as it , It's time to equals Method to screen .
① If two objects equals equal , So the two objects HashCode It must be the same
② If I have two objects HashCode identical , Doesn't mean two objects are the same , It can only indicate that these two objects are in the hash storage structure , Store in the same location
4. Why rewrite equals It needs to be rewritten hashcode?
such as : There is one A Class overridden equals Method , But no rewriting hashCode Method , Look at the output , object a1 And the object a2 Use equals The method is equal , According to the above hashcode Usage of , So the two of them hashcode Definitely equal , But because there's no rewriting here hashcode Method , Two of them hashcode Not the same. , therefore , We're rewriting equals After the method , Try to rewrite it as well hashcode Method , Through a certain algorithm , Make them in equals When equal , There will be the same hashcode value .
Last , Let's take a look String At the bottom hashcode Source code

边栏推荐
- @Retryable @backoff @recover retry the use of annotations
- Mgre.hdlc.ppp.chap.nat comprehensive experiment
- JS foundation -- JSON
- Analysis of FLV packaging
- Redux best practices "Redux toolkit"
- Technical experts from large factories: Thoughts on cloud nativity and software supply chain security
- DOM node type
- Use unicloud cloud function to decode wechat motion steps in applet
- TS uses a third-party library, and there is no type declaration file error handling
- Dynamic programming -- Digital DP
猜你喜欢

B2B e-commerce trading platform of heavy metal industry: breaking the state of data isolation and improving the benefits of heavy metal industry

What are you working for? Don't give up is our only choice, come on, everyone

Pycharm writes SQLite statements without code prompts

JS written test -- regular expression

How to use blender to make 360 degree panorama and panoramic video?

Get to know string thoroughly

R language one page and many pictures

Mid year summary and personal feelings
![[TinyML]EfficientFormer:Vision Transformers at MobileNet Speed](/img/e7/b9ecf49721e6304a2d8ca824b64458.png)
[TinyML]EfficientFormer:Vision Transformers at MobileNet Speed

Pagoda workman WSS reverse proxy socket legal domain name applet chat remove port
随机推荐
Threat report in June: new bank malware malibot poses a threat to mobile banking users
Matlab for circular pit
Resolved (the latest version of selenium reported an error) attributeerror: module 'selenium webdriver‘ has no attribute ‘PhantomJS‘
JS written test question -- deep copy of object
C language: Structure -- a detailed explanation of memory byte alignment
Strategy mode, just read one article
Rotating frame target detection mmrotate v0.3.1 training hrsc2016 data set (II)
Text reading end judgment
Arduino + si5351 square wave generator
6. Object storage
Keil compile download error: no algorithm found for: 08000000h - 08001233h solution
Apk packaging process
Rolling division, Young's matrix and three-step flip
JS foundation -- task queue and event loop
[TinyML]EfficientFormer:Vision Transformers at MobileNet Speed
Use unicloud cloud function to decode wechat motion steps in applet
Tensorflow's study notes (I)
On Calc optimization of calcite
Tp5.1 paging (with parameter transfer)
HAC cluster is modified to stand-alone