Java集合HashSet的hashcode方法引起的記憶體流失問題

來源:互聯網
上載者:User

package apistudy;<br />import java.util.HashSet;<br />import java.util.Set;<br />/**<br /> * Created on 2010-6-7<br /> * <p>Description: [HashSet的hashcode方法引起的記憶體流失]</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("唐僧","pwd1",25);<br /> Person p2 = new Person("孫悟空","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("總共有:"+set.size()+" 個元素!"); //結果:總共有:3 個元素!<br /> p3.setAge(2); //修改p3的年齡,此時p3元素對應的hashcode值發生改變<br /> set.remove(p3); //此時remove不掉,造成記憶體流失<br /> set.add(p3); //重新添加,居然添加成功<br /> System.out.println("總共有:"+set.size()+" 個元素!"); //結果:總共有:4 個元素!<br /> for (Person person : set)<br /> {<br /> System.out.println(person);<br /> }</p><p> }<br />}<br />  

運行結果:

 

總共有:3 個元素!

總共有:4 個元素!

豬八戒-->pwd3-->2

孫悟空-->pwd2-->26

唐僧-->pwd1-->25

豬八戒-->pwd3-->2

總結: HashSet中,在對應元素添加進set集合後,不要再去修改元素的值,否則對應元素的hashcode值發生變化,此時如果調用 集合的remove(),contains()方法,將不會得到正確的結果。remove()方法並不能正確remove掉對應的元素,造成記憶體流失。

 

 

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 setUsername(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 />   

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.