py超级菜鸟 发表于 2012-09-30 14:15

HashSet 的疑问


import java.util.*;
class Student{
        public String name;
        public int id;
       
       
       
        public Student(String name, int id) {
                super();
                this.name = name;
                this.id = id;
        }

        public String toString()
        {
                return "id:" + id + " name:" + name;
        }
       
        public int hasCode()
        {
                return name.hashCode()^id;
        }
       
        public boolean equals(Object obj)
        {
                if(obj instanceof Student)
                {
                        Student s = (Student)obj;
                        if(s.id == this.id && s.name.equals(this.name))
                        return true;
                }
                return false;
        }
       
}

public class Test{
        public static void main(String[] args)
        {
                int count = 0;
                HashSet<Student> h = new HashSet<Student>();
                Student s1 = new Student("sb3",1002);
                Student s2 = new Student("sb2",1001);
                Student s3 = new Student("sb3",1002);
                System.out.println(s1.hasCode());
                System.out.println(s3.hasCode());
                System.out.println(s1.equals(s3));
                h.add(s1);
                h.add(s3);       
                h.add(s2);
                Iterator<Student> it = h.iterator();
                while(it.hasNext()){               
                                System.out.println("num"+ count + ":" );
                                System.out.println(it.next());
                                count++;
                }
        }
}
上面是我的代码
运行结果:
112686
112686
true
num0:
id:1002 name:sb3
num1:
id:1002 name:sb3
num2:
id:1001 name:sb2
我想问HashSet 是怎么判断两个加入的对象是否和已经存在的对象相同呢?难道不是通过对象本身的equals和hashcode 吗 ?
这里s1和s3的hashcode相同 且equals返回true怎么还能加入到HashSet中呢?求大侠解答 非常感谢

rising-star 发表于 2012-10-11 13:20

项目:电信的项目、银行项目==
周期:长期
地点:北京、深圳、广州
工资:发简历给我时,请提一下&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;

现在诚招:
1、PHP高级开发工程师(团队经理1人)
&nbsp; &nbsp;工作内容:
&nbsp; &nbsp;——开发门户的PHP界面部分框架,
&nbsp; &nbsp;——负责带领小团队开发人员进行协调和开发
&nbsp; &nbsp;——实现核心业务团队设计的业务逻辑。
&nbsp; &nbsp;招聘要求:
&nbsp; &nbsp;——负责维护、搭建开发框架
&nbsp; &nbsp;——需要熟悉Linux系统、Web网站的各个方面如负载均衡原理、HTTP协议
&nbsp; &nbsp;——Restful API、Web Service、消息队列AMQP、Memcache等
&nbsp; &nbsp;——能通过Extension扩展PHP,熟悉Yii Framewrok、ThinkPHP等常用框架

2、PHP开发工程师(4人)
&nbsp; &nbsp;工作内容:根据PHP开发框架实现界面展示等业务逻辑
&nbsp; &nbsp;招聘要求:
&nbsp; &nbsp;——熟悉PHP和web开发,熟悉HTML、CSS、jQuery
&nbsp; &nbsp; ——熟悉REST API,熟悉Web Service

3、JAVA高级开发工程师&nbsp;&nbsp;(3人=团队经理)
&nbsp; &nbsp;工作内容及招聘需求:
&nbsp; &nbsp;——开发业务系统的部分框架
&nbsp; &nbsp;——负责带领小团队开发人员进行协调和开发,实现核心业务设计团队设计的逻辑&nbsp; &nbsp;
&nbsp; &nbsp;——负责维护、搭建JAVA开发框架
&nbsp; &nbsp;——需要熟悉SSH企业级开发的内核源码级结构,
&nbsp; &nbsp;——熟悉企业级应用集成模式和常见设计模式,
&nbsp; &nbsp;——熟悉系统开发的各个方面如数据库、缓存、数据结构、算法、网络协议、WebService、消息队列、多线程、Linux部署等。
&nbsp; &nbsp;——熟悉Spring、iBatis、Oracle、MySQL等常见开发

4、JAVA业务逻辑开发 (9人=高级开发工程师)
&nbsp; &nbsp;工作内容及招聘需求:
&nbsp; &nbsp;——针对业务逻辑框架进行逻辑开发,保证代码风格一致、稳定正确
&nbsp; &nbsp;——熟悉SSH编程框架
&nbsp; &nbsp;——熟练掌握JAVA编程的基本原理,
&nbsp; &nbsp;——熟悉设计模式,掌握单元测试
&nbsp; &nbsp;——熟悉消息队列、REST等API接口。

另外:此项目还招
&nbsp; &nbsp;&nbsp; &nbsp;——UI前端交互设计的高级人才(3人)
将美工设计完成的页面进行切分,开发前端页面,
制作网页模板,完成业务模块的用户交互部分。
要求:熟悉HTML5、CSS和jQuery等前端开发技术,
熟悉RESTful API调用,
熟悉Photoshop等常见网页设计工具的使用。
&nbsp; &nbsp;&nbsp; &nbsp;
——Web原型设计的高级人才 (1人)
实现用户自服务门户、管理员门户中的交互逻辑高仿界面原型
要求:熟悉常用UI和UED设计方法和思路,
熟练掌握需求分析、原型设计等技能,
熟悉管理系统原型界面开发,
熟悉Web界面设计原理,
熟悉Axure RP、Photoshop等原型工具的使用

有兴趣的朋友同学们,发个邮件回复我:hr@rising-star.us 或 278092804@qq.com
精英汇:QQ群 244203161 (500人)
页: [1]
查看完整版本: HashSet 的疑问