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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 棉/mg花/x糖 中级黑马   /  2013-6-27 11:30  /  3666 人查看  /  23 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 孙百鑫 于 2013-7-1 07:43 编辑

题目:自定义一个Person类(有id,姓名,年龄,生日,电话等属性),并创建一些Person实例添加到HashSet集合中,Person对象的id一致则为重复。

评分

参与人数 1技术分 +1 收起 理由
孙百鑫 + 1 神马都是浮云

查看全部评分

23 个回复

倒序浏览
保证唯一要复写hashCode和equals。你这个把equals复写就OK了。
回复 使用道具 举报
public boolean equals(Student stu)
{
if(stu.getId()==this.getId())
    return true;
else
   return false;
}
回复 使用道具 举报
王靖远 发表于 2013-6-27 11:38
public boolean equals(Student stu)
{
if(stu.getId()==this.getId())

求完整源码。我马上上交,谢谢~~来不及思考
回复 使用道具 举报
  1. class Person
  2. {
  3.         private int id;
  4.         private int age;
  5.         private String name;
  6.         private int tel;
  7.         Student(int id,int age,String name,int tel)
  8.         {
  9.                 this.id = id;
  10.                 this.age = age;
  11.                 this.name = name;
  12.                 this.tel = tel;
  13.         }
  14.         //get set就不写了

  15.         public int hashCode()
  16.         {
  17.                 return Integer.parseInt(name)+id+age+tel*33;
  18.         }
  19.         public boolean equals(Student stu)
  20.         {
  21.                 int num = new Integer(stu.getID()).compareTo(new Integer(this.getID()))
  22.                 if(num==0)
  23.                         return true;
  24.                 else
  25.                         return false;
  26.         }
  27. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
孙百鑫 + 1 很给力!

查看全部评分

回复 使用道具 举报

呵呵,别呀~~谢了哈,完整的~~
回复 使用道具 举报
  1. package test;

  2. import java.util.Arrays;
  3. import java.util.Date;
  4. import java.util.HashSet;


  5. public class Test {

  6. /**
  7. * 题目:自定义一个Person类(有id,姓名,年龄,生日,电话等属性),并创建一些Person实例添加到HashSet集合中,Person对象的id一致则为重复。
  8. */
  9. public static void main(String[] args) {
  10. Person p1=new Person("001","zhangsan",11,new Date(System.currentTimeMillis()),"123456789");
  11. Person p2=new Person("002","zhangsan",11,new Date(System.currentTimeMillis()),"123456789");
  12. Person p3=new Person("001","zhangsan",11,new Date(System.currentTimeMillis()),"123456789");
  13. HashSet<Person> hs = new HashSet<Person>();
  14. hs.add(p3);
  15. hs.add(p2);
  16. hs.add(p1);
  17. System.out.println(hs.size());//2
  18. }

  19. }
  20. class Person{
  21. private String id;
  22. private String name;
  23. private int age;
  24. private Date birthday;
  25. private String phone;

  26. public Person(String id,String name,int age,Date birth,String phone){
  27. this.id=id;
  28. this.name=name;
  29. this.age=age;
  30. this.birthday=birth;
  31. this.phone=phone;
  32. }
  33. @Override
  34. public int hashCode(){
  35. return id.hashCode();
  36. }

  37. @Override
  38. public boolean equals(Object obj){
  39. Person p =(Person)obj;
  40. if(this.id.equals(p.id)){
  41. return true;
  42. }
  43. return false;

  44. }
  45. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
孙百鑫 + 1 赞一个!

查看全部评分

回复 使用道具 举报
这都可以!!!
回复 使用道具 举报
记住用到hash的就要复写他的hashcode和equals方法。用这两个方法来判断是否是重复元素。建议自己动手写写。如果写不出来再看别人怎么写的。
回复 使用道具 举报
对  就是这样
回复 使用道具 举报
C:\Users\Administrator\Desktop
回复 使用道具 举报
回复 使用道具 举报

咋弄一个路径呀~~~
回复 使用道具 举报 1 0
import java.util.HashSet;
import java.util.Iterator;

/**
* 题目:自定义一个Person类(有id,姓名,年龄,生日,电话等属性),并创建一
* 些Person实例添加到HashSet集合中,Person对象的id一致则为重复。

* @author Administrator
*
*/
public class Person {

       
        @Override
        public String toString() {
               
               
                return this.getName();
        }
        public String getName() {
                return this.name;
        }
        public void setName(String name) {
                this.name = name;
        }
        public int getAge() {
                return age;
        }
        public void setAge(int age) {
                this.age = age;
        }
       
        public String getTelnumber() {
                return telnumber;
        }
        public void setTelnumber(String telnumber) {
                this.telnumber = telnumber;
        }
        public int getId() {
                return this.id;
        }
        public void setId(int id) {
                this.id = id;
        }
        @Override
        public int hashCode() {//覆写hashCode方法
               
                return this.id;
        }
        @Override
        public boolean equals(Object obj) {//覆写equals方法
                Person p=(Person)obj;
                System.out.println(this.toString()+"...equals...."+p.toString());
                return this.getId()-p.getId()==0;
               
        }
        private int id;
        private String name;
        private int age;       
        private String telnumber;
        public Person(int id, String name, int age, String telnumber) {
                super();
                this.id = id;
                this.name = name;
                this.age = age;
              this.telnumber = telnumber;
        }
        public static void main(String []args){
                HashSet<Person> hs=new HashSet<Person>();
                hs.add(new Person(123,"张三",23,"021-43560980"));
                hs.add(new Person(123,"李四",22,"010-43567870"));//李四和张三的ID相同,没有添加进集合。
                hs.add(new Person(112,"王五",21,"022-43534590"));
                hs.add(new Person(156,"小明",20,"021-43561230"));
               
                Iterator<Person> it=hs.iterator();
                while(it.hasNext()){
                        Person p=it.next();
                        System.out.println(p.getId()+"::"+p.getName()+"::"+p.getAge()+"::"+p.getTelnumber());
                }
        }

}
结果:
李四...equals....张三


156::小明::20::021-43561230
112::王五::21::022-43534590
123::张三::23::021-43560980

评分

参与人数 1技术分 +1 收起 理由
孙百鑫 + 1 赞一个!

查看全部评分

回复 使用道具 举报

本来发桌面上的一个图片的       居然只有路径发成功了
回复 使用道具 举报
你这个是入学测试??!!
回复 使用道具 举报
花伟昌 发表于 2013-6-27 17:24
import java.util.HashSet;
import java.util.Iterator;

嗯,太感激了~~
回复 使用道具 举报
如果是入学测试题 最好自己做   论坛发问 消息扣分哦
回复 使用道具 举报
刘凯 发表于 2013-6-28 11:15
如果是入学测试题 最好自己做   论坛发问 消息扣分哦

嗯,这个不是入学测试,是在百度文库挡下来的一套试题~~~谢谢^_^
回复 使用道具 举报
棉/mg花/x糖 发表于 2013-6-28 23:03
嗯,这个不是入学测试,是在百度文库挡下来的一套试题~~~谢谢^_^

我还没走到入学测试那一关啊,咔咔~~~
回复 使用道具 举报
12下一页
您需要登录后才可以回帖 登录 | 加入黑马