免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2137 | 回复: 1
打印 上一主题 下一主题

求助!关于LinkedHashSet的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-05-04 15:34 |只看该作者 |倒序浏览
在使用LinkedHashSet的过程中发现了一个问题
在LinkedHashSet有一个元素,我对这个元素进行了修改,hashcode发生了变化。
但是修改后,用LinkedHashSet的contains方法判断新的元素是否存在时却查找不到。
代码如下:
public class Welcome {
    public static void main(String[] args) {
        ttt test1 = new ttt();
        test1.setAaa("test1aaa");
        test1.setBbb("test1bbb");
        ttt test2 = new ttt();
        test2.setAaa("test2aaa");
        test2.setBbb("test2bbb");
        Set<ttt> tttList = new LinkedHashSet<ttt>();
        tttList.add(test1);
        tttList.add(test2);
        ttt test3 = new ttt();
        test3.setAaa("test2aaa");
        test3.setBbb("test2bbb");
        System.out.println("test3");
        System.out.println(test3.hashCode());
        
        if (tttList.contains(test3) == true){
            System.out.println("find111111111");
        }
        
        Iterator<ttt> alarmItor = tttList.iterator();
        while ( alarmItor.hasNext() == true ) {
            ttt tempAlarm = alarmItor.next();
            System.out.println(tempAlarm.hashCode());
            if (tempAlarm.equals(test3) == true){
                tempAlarm.setAaa("test444");
                System.out.println("change");
                System.out.println(tempAlarm.hashCode());
            }
        }
        
        System.out.println(tttList.size());
        
        ttt test4 = new ttt();
        test4.setAaa("test444");
        test4.setBbb("test2bbb");
        System.out.println(test4.hashCode());
        
        
        System.out.println(test2.hashCode());
        if (tttList.contains(test2) == true){
            System.out.println("find222222222");
        }
        
        Iterator<ttt> alarmItor1 = tttList.iterator();
        while ( alarmItor1.hasNext() == true ) {
            ttt tempAlarm = alarmItor1.next();
            System.out.println(tempAlarm.getAaa());
            System.out.println(tempAlarm.getBbb());
            alarmItor1.remove();
            if (tempAlarm.equals(test4) == true){
                System.out.println("find3333333333");
            }
        }
        
        System.out.println(tttList.size());
        
        Iterator<ttt> alarmItor2 = tttList.iterator();
        while ( alarmItor2.hasNext() == true ) {
            ttt tempAlarm = alarmItor2.next();
            System.out.println(tempAlarm.getAaa());
            System.out.println(tempAlarm.getBbb());
        }
        
    }
}

输出结果如下:
test3
1924912066
find111111111
1923958754
1924912066
change
1999371873
2
1999371873
1999371873
test1aaa
test1bbb
test444
test2bbb
find3333333333
1
test444
test2bbb

论坛徽章:
0
2 [报告]
发表于 2008-05-06 15:08 |只看该作者
给出ttt的定义看下
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP