The ninth day of the dark horse programmer

Source: Internet
Author: User

---------- Android training, Java training, and hope to communicate with you!
----------

Comparison of hashset and analysis of hashcode

  • Hashset implements the set interface, which is supported by the hash table. It does not guarantee the set iteration sequence; in particular, it does not ensure that the sequence remains unchanged. This class is allowed
    NullElement.
  • Hashcode in Java, the hash code represents the characteristics of an object. Hash code generation basis: the hash code is not completely unique. It is an algorithm that allows objects of the same class to have different hash codes according to their own characteristics, but it does not indicate that the hash codes of different objects are completely different. The same is true for programmers who want to write hash code algorithms. The following describes three common hash algorithms. 1: hashcode of the object class. The structure of the memory address of the returned object after processing. Because the memory address of each object is different, the hash code is also different. 2: hashcode of the string class. According to the content of the string contained in the string class, a hash code is returned based on a special algorithm. As long as the string content is the same, the returned hash code is also the same. 3: For the integer class, the returned hash code is the value of the integer contained in the integer object. For example, integer I1 = new INTEGER (100) and i1.hashcode is 100. It can be seen that two integer objects of the same size return the same hash code.

The following is an example:

String str1 = "AA", str1.hashcode = 3104 string str2 = "BB", str2.hashcode = 3106 string str3 = "AA", str3.hashcode = 3104

According to hashcode, str1 can be obtained! = Str2, str1 = str3 so when you create an object, because you change the value of the object, the hash value of the object will be changed. If you delete this object, in fact, you deleted the hash value you modified later. The Hash Value of the previous object still exists in the memory, leading to memory leakage.

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.