How to use hash and hash
Learning requires a system, but the system learning is well developed by yourself.
Learning that does not rely on the business, does not suit the work, and does not practice is ineffective. Therefore, my blog is like this: I don't write XXX series specifically, but write something when I think of anything or what I use at work.
This is because, when I got off work today, I walked back from the company and chatted with colleagues for a long time. So let's talk about hash today.
(It's so capricious that you can write anything you think. This is because it has nothing to do with half a cent of money. Haha ......)
The hashcode () in the object is the memory address ing (not rigorous, but easy to remember, unique ). Equals () is used to compare whether the object's memory address ing is equal.
Principle: If two objects are equal, the hashcode of the two objects should be equal. Therefore, to override equals, you must overwrite the hashcode.
Hash-related Java APIs, hashMap, hashSet, and other hash sets are internally built with their own hash chains used to index corresponding hash sets, making traversal more efficient.
If the equals of an object is equal and the hashcode is not equal, an error occurs when the hash set adds, deletes, or modifies elements.
The reason is (for example, delete): Delete the elements in the hash set,
1: Calculate the hashcode of the element.
2: Use the obtained hashcode to search in the index chain of the hash set (instead of the traversal type, the matching type is used, and intelligence is compared to similar areas in the index)
3: If the equals of an object is the same, but the hashcode is not the same, the deletion cannot be completed. Memory leakage.
The rest, you know.
Please throw bricks and correct them. Thank you ~