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