Memory leakage caused by the hashcode method of the hashset in Java

Source: Internet
Author: User

Package apistudy; <br/> Import Java. util. hashset; <br/> Import Java. util. set; <br/>/** <br/> * created on 2010-6-7 <br/> * <p> description: [Memory leakage caused by hashcode of hashset] </P> <br/> * @ author shixing_11@sina.com <br/> * @ version 1.0 <br/> */<br/> public class hashsettest <br/>{< br/> Public static void main (string [] ARGs) <br/>{< br/> set <person> set = new hashset <person> (); <br/> person p1 = new person (" Tang Seng "," pwd1 ", 25); <br/> person P2 = new person (" Sun Wukong "," pwd2 ", 26 ); <br/> person P3 = new person ("", "pwd3", 27); <br/> set. add (P1); <br/> set. add (P2); <br/> set. add (P3); <br/> system. out. println ("Total:" + set. size () + "elements! "); // Result: There are three elements in total! <Br/> p3.setage (2); // modify the age of P3. The hashcode value corresponding to the P3 element changes. <br/> set. remove (P3); // at this time, the removal fails, causing memory leakage <br/> set. add (P3); // re-add, actually added successfully <br/> system. out. println ("Total:" + set. size () + "elements! "); // Result: There are 4 elements in total! <Br/> for (person: Set) <br/>{< br/> system. out. println (person); <br/>}</P> <p >}< br/>

Running result:

 

Total: 3 elements!

Total: 4 elements!

Pig 8 --> pwd3 --> 2

Sun Wukong --> pwd2 --> 26

Tang Seng --> pwd1 --> 25

Pig 8 --> pwd3 --> 2

Summary: In a hashset, do not modify the value of an element after the corresponding element is added to the Set set. Otherwise, the hashcode value of the corresponding element changes. If The remove () and contains () Methods of the set will not get the correct results. The remove () method does not correctly remove the corresponding elements, causing memory leakage.

 

 

Person. Java

Package apistudy; <br/> public class person <br/>{< br/> private string username; <br/> private string password; <br/> private int age; <br/> Public Person (string username, string password, int age) <br/>{< br/> This. username = username; <br/> This. password = password; <br/> This. age = age; <br/>}< br/> Public String GetUserName () <br/>{< br/> return username; <br/>}< br/> Public void setusernam E (string username) <br/>{< br/> This. username = username; <br/>}< br/> Public String GetPassword () <br/>{< br/> return password; <br/>}< br/> Public void setpassword (string password) <br/>{< br/> This. password = password; <br/>}< br/> Public int getage () <br/>{< br/> return age; <br/>}< br/> Public void setage (INT age) <br/>{< br/> This. age = age; <br/>}< br/> @ override <br/> Public int Hashcode () <br/>{< br/> final int prime = 31; <br/> int result = 1; <br/> result = prime * result + age; <br/> result = prime * result + (Password = NULL )? 0: password. hashcode (); <br/> result = prime * result + (username = NULL )? 0: username. hashcode (); <br/> return result; <br/>}< br/> @ override <br/> Public Boolean equals (Object OBJ) <br/>{< br/> If (this = OBJ) <br/> return true; <br/> If (OBJ = NULL) <br/> return false; <br/> If (getclass ()! = Obj. getclass () <br/> return false; <br/> Person Other = (person) OBJ; <br/> If (age! = Other. Age) <br/> return false; <br/> If (Password = NULL) <br/>{< br/> If (other. Password! = NULL) <br/> return false; <br/>}< br/> else if (! Password. equals (Other. password) <br/> return false; <br/> If (username = NULL) <br/>{< br/> If (Other. username! = NULL) <br/> return false; <br/>}< br/> else if (! Username. equals (Other. username) <br/> return false; <br/> return true; <br/>}< br/> @ override <br/> Public String tostring () <br/>{< br/> return this. username + "-->" + this. password + "-->" + this. age; <br/>}</P> <p >}< br/>

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.