public HashObject(int ax, int ay, int az){
hashx = ax;
hashy = ay;
z = az;
}
public void setHashx(int ax){hashx = ax;}
public void setZ(int az){z = az;}
/*右键-Source-generate hashCode() and equals()*/
public int hashCode(){
final int prime = 31;
int result = 1;
result = prime * result + hashx;
result = prime * reuslt + hashy;
return result;
}
}
//main
Collection collection = new HashSet();
HashObject hashObj1 = new HashObject(1,2,3);
HashObject hashObj2 = new HashObject(2,3,4);
HashObject hashObj3 = new HashObject(3,4,5);
HashObject hashObj4 = new HashObject(1,2,3);
collection.add(hashObj1);
collection.add(hashObj2);
collection.add(hashObj3);
collection.add(hashObj4);
collection.remove(j4); //Success
System.out.println(collention.size()); //output: 3
hashObj1.setHashX(3);
collection.remove(j1); //Fail,内存泄漏
System.out.println(collention.size()); //output: 3
技术要点:1、使用反射和配置文件优化上面的代码Collection collection = new HashSet();