Dark Horse programmer--hashcode memory leak caused by

Source: Internet
Author: User

The role of the Hashcode method: When an object is to be stored in a hash set, the JVM first calls the Hashcode method to get the hash value of the object, then finds the corresponding storage area based on the hash value, and finally takes out all the elements of the region compared with the object, if equal, Do not deposit the element, otherwise, deposit. This improves the efficiency of the lookup by not having to traverse all the elements in the collection to get the results we want. However, if you do not overwrite the Hashcode method, the same objects may be stored in the HashSet collection, although they are the same as equals, but their memory areas are different, they will not be compared with equals. In order for two identical objects to be stored in the same area, Java has one rule: as long as the equals of the two objects are the same, their hashcode should be the same. The premise of overwrite the Hashcode method is that the set that the object is stored in is a hash set, otherwise, the Hashcode method has no value, the Hashcode method may also cause a memory leak, if an object has been stored in the HashSet collection, Can not modify those involved in the calculation of the hash value of the field, otherwise, the modified hash value and the original hash value is not the same, so when using the Equals method to compare, can not find the original object, so that the hashset element can not be deleted, resulting in a memory leak.

Class hashcode{

public static void Main (String [] args) {

Collection Collection = new HashSet ();

Person Person1 = new Person ("Zhangsan", 24);

Person Person2 = new Person ("Lisi", 25);

Person Person3 = new Person ("Zhangsan", 24);

Collection.add (Person1);

Collection.add (Person2);

Collection.add (Person3);

Collection.add (Person1);

System.out.println (Collection.size ());

} }

The result of the output is 3 because there cannot be duplicate elements in the set collection.

We note, however, that the two objects of the Person1 and Person2 are the same, but because the Equals method is not covered in the person class, the two objects are different.

So, as long as the hash set, we should overwrite the Equals method and the Hashcode method, but if the value of the Equals method is covered, the result can be 2 or 3, because there is no overwrite hashcode method, When Person3 is added to the HashSet collection, the hash values obtained by the JVM call to the Hashcode method may or may not be equal, because equals is equal, so if the hashes are equal, the two objects are stored in the same area, the result is 2, If they are not equal, the two objects are stored in a different region, and equals does not find an equal object in the range, so the result is 3,

Dark Horse programmer--hashcode memory leak caused by

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.