public class ReflectPoint {
public String str1;
public int i;
public String str2="asdfghaaa";
public ReflectPoint(String str1, int i) {
super();
this.str1 = str1;
this.i = i;
}
public String toString()
{
return str1+":"+str2;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + i;
result = prime * result + ((str1 == null) ? 0 : str1.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
ReflectPoint other = (ReflectPoint) obj;
if (i != other.i)
return false;
if (str1 == null) {
if (other.str1 != null)
return false;
} else if (!str1.equals(other.str1))
return false;
return true;
}
}
package com.baidu.first;
import java.util.*;
public class ReflectText1 {
public static void main(String[] args){
Collection collection=new HashSet();
ReflectPoint rft1=new ReflectPoint("asd",3);
ReflectPoint rft2=new ReflectPoint("asdf",4);
ReflectPoint rft3=new ReflectPoint("asds",3);
collection.add(rft1);
collection.add(rft2);
collection.add(rft3);
collection.add(rft1);
rft1.i=8;//哈希集合中的元素被修给后该对象就移除不掉了,这就叫内存泄露也叫做内存渗漏
collection.remove(rft1);
System.out.println(collection.size());
}