Java中的HashCode(2)之Hashset造成的内存泄露

所谓内存泄露就是一个对象占用的一块内存,当这个对象不在被使用时,该内存还没有被收回。

 

例子

package cn.xy.test;

public class Point2
{
 private int x;
 private int y;

 public Point2(int x, int y)
 {
  super();
  this.x = x;
  this.y = y;
 }

 @Override
 public int hashCode()
 {
  final int prime = 31;
  int result = 1;
  result = prime * result + x;
  result = prime * result + y;
  return result;
 }

 @Override
 public boolean equals(Object obj)
 {
  if (this == obj) return true;
  if (obj == null) return false;
  if (getClass() != obj.getClass()) return false;
  Point2 other = (Point2) obj;
  if (x != other.x) return false;
  if (y != other.y) return false;
  return true;
 }

 public int getX()
 {
  return x;
 }

 public void setX(int x)
 {
  this.x = x;
 }

 public int getY()
 {
  return y;
 }

 public void setY(int y)
 {
  this.y = y;
 }

}

public class HashSetAndHashCode2
{
 public static void main(String[] args)
 {
  HashSet<Point2> hs2 = new HashSet<Point2>();
  Point2 p21 = new Point2(3, 3);
  Point2 p22 = new Point2(3, 5);
  hs2.add(p21);
  hs2.add(p22);
  p22.setY(7);
  hs2.remove(p22);
  System.out.println(hs2.size());
 }
}

很多人认为打印出的结果是1。结果是2。为什么呢?当一个对象被存储在Hashset中后,如果修改参与计算hashcode有关的字段,那么修改后的hashcode的值就与一开始存储进来的hashcode的值不同了,这样contains无法通过hashcode找到该元素,所以无法删除。这就告诉我们,当一个对象被存储在Hashset中后,不要修改与计算hashcode有关的字段。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值