Why the Set\hashset collection can go heavy

Source: Internet
Author: User

If you want to find out if a collection contains an object, how does the approximate program code write? When an element is found to be equal to the result of the Equals method comparison of the object being looked up, it stops continuing to find and returns the positive information, otherwise the negative information is returned. If there are many elements in a collection, such as 10,000 elements, and there is no object to look for, it means that your program needs to take 10,000 elements out of the set for a peck comparison to get a conclusion, someone invented a hashcode algorithm to improve the efficiency of the search, This way the collection is divided into several storage areas, each object can calculate a hash code, the hash code can be grouped, each group corresponding to a storage area, based on the hash code of an object can determine the object should be stored in which region,

HashSet is to use a hash algorithm to store a collection of objects, which internally uses a number N The storage area where the hash code is grouped by the way the redundancy is taken. the hashcode in object is used to return the hash code of the Java object, thus improving the efficiency of the lookup.

In order to ensure that instance objects of a class can be stored properly in HashSet, it is required that the two instance objects of this class be equal to the result of the Equals () method, and their hash code must also be equal, that is, if the result of Obj1.equals (OBJ2) is true, Then the result page of the expression should be true.

Obj1.hashcode () = = Obj2.hashcode ()

If the Hashcode () method of a class does not follow the above requirements, then, when the two instance objects are equal in comparison with the Equals () method, they should not be stored in the set collection at the same time, but if they are stored in the HashSet collection, Because the return value of their hashcode () method is different, the second object may be placed in a different area than the first object first, so it is not possible to compare the Equals method with the first object and it may be stored in the HashSet collection. The Hashcode () method of the object class does not satisfy the requirement that the objects be stored in the HashSet, because the return value is derived from the memory address of the object, and the hash value returned by the same object at any time during the run of the program is always constant, so As long as there are two different instance objects, the return value of their default Hashcode method is different, even if their equals method compares the results equally.

Tips:

(1) Generally, the two instance objects of a class are equal when compared with the Equals () method, and their hash codes must be equal, but the opposite is not true, that is, the Euqlas method compares unequal objects with the same hash code, Or the result of the Equals method comparison of two objects with the same hash code can be unequal, for example, the Euqals method of the string "BB" and "Aa" will certainly not be equal, but their Hashcode method return values are equal.

(2) When an object is stored in the HashSet collection, it is not possible to modify the fields in the object that participate in the hash value, otherwise the hash value after the object has been modified is different from the hash value originally stored in the HashSet collection, in which case Even if the Cantains method uses the object's current reference as a parameter to retrieve an object in the HashSet collection, the result of the object cannot be found. This also causes the current object to be deleted separately from the HashSet collection, resulting in a memory leak.

Why the Set\hashset collection can go heavy (turn)

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.