A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 任艳旭 中级黑马   /  2012-9-13 15:13  /  1444 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 任艳旭 于 2012-9-15 11:55 编辑

老师讲过一个关于内存泄漏的内容,
代码:public class ReflectPoint {
        private Date birthday = new Date();
        
        private int x;
        public int y;
        
        public ReflectPoint(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;
                final ReflectPoint other = (ReflectPoint) obj;
                if (x != other.x)
                        return false;
                if (y != other.y)
                        return false;
                return true;
        }


        @Override
        public String toString(){
                return str1 + ":" + str2 + ":" + str3;
        }


        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 Date getBirthday() {
                return birthday;
        }


        public void setBirthday(Date birthday) {
                this.birthday = birthday;
        }
        
}
public class ReflectTest2 {

        public static void main(String[] args) throws Exception{        
                Collection collections = new HashSet();
                ReflectPoint pt1 = new ReflectPoint(3,3);
                ReflectPoint pt2 = new ReflectPoint(5,5);
                ReflectPoint pt3 = new ReflectPoint(3,3);        

                collections.add(pt1);
                collections.add(pt2);
                collections.add(pt3);
                collections.add(pt1);        
               
                pt1.y = 7;                //更改y的值
                collections.remove(pt1);//这里不能移除为啥
               
                System.out.println(collections.size());
        }
}
运行完的结果是集合长度是2,如果把这两句代码删掉,pt1.y = 7;collections.remove(pt1);运行结果集合长度也是2,为什么不能移除啊?

3 个回复

倒序浏览
因为pt1已经改了,删除的时候是依据pt1的,参数的pt1和Collection中的pt1不一样,找不到了,无法删除。
回复 使用道具 举报
HashSet中,在对应元素添加进set集合后,不要再去修改元素的值,否则对应元素的hashcode值发生变化,此时如果调用
集合的remove(),contains()方法,将不会得到正确的结果。remove()方法并不能正确remove掉对应的元素,造成内存泄漏。
回复 使用道具 举报
当一个对象存储进HashSet集合中就不能修改这个对象中那些参与计算哈希值的字段了。
否则对象修改后的哈希值与最初存储进HashSet集合中时的值就不相同了,
这就导致了无法从HashSet集合中单独删除当前对象。这样也就会造成内存泄露
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马