How to use hash and hash

Source: Internet
Author: User

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 ~

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.