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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

a
  1. package setdemo;

  2. import java.util.HashSet;

  3. /**
  4. * 需求:存储自定义对象,并保证元素的唯一性
  5. * 要求:当对象的成员变量值都相同,则认为是一个元素
  6. */
  7. public class HashSetDemo1 {

  8.         /**
  9.          * @param args
  10.          */
  11.         public static void main(String[] args) {
  12.                 // TODO Auto-generated method stub
  13.                 //创建哈希set存储对象
  14.                 HashSet<Student> hs = new HashSet<Student>();
  15.                 //创建学生对象
  16.                 Student s1 = new Student("Lili", 15);
  17.                 Student s2 = new Student("Lucy", 15);
  18.                 Student s3 = new Student("Lala", 8);
  19.                 Student s4 = new Student("Lili", 17);
  20.                 Student s5 = new Student("Cici", 15);
  21.                 Student s6 = new Student("Lili", 15);
  22.                 //添加对象到集合
  23.                 System.out.println("添加对象:"+hs.add(s1));
  24.                 System.out.println("添加对象:"+hs.add(s2));
  25.                 System.out.println("添加对象:"+hs.add(s3));
  26.                 System.out.println("添加对象:"+hs.add(s4));
  27.                 System.out.println("添加对象:"+hs.add(s5));
  28.                 System.out.println("添加对象:"+hs.add(s6));
  29.                 //遍历输出对象
  30.                 for (Student s : hs) {
  31.                         System.out.println(s.getName()+"..."+s.getAge()+"...hashCode:\t"+s.hashCode());
  32.                 }
  33.         }

  34. }
复制代码
  1. package setdemo;

  2. public class Student {
  3.         private String name;
  4.         private int age;
  5.         public Student(String name, int age) {
  6.                 super();
  7.                 this.name = name;
  8.                 this.age = age;
  9.         }
  10.         public String getName() {
  11.                 return name;
  12.         }
  13.         public void setName(String name) {
  14.                 this.name = name;
  15.         }
  16.         public int getAge() {
  17.                 return age;
  18.         }
  19.         public void setAge(int age) {
  20.                 this.age = age;
  21.         }
  22.         //重写hashCode()和toString()方法
  23.         @Override
  24.         public int hashCode() {
  25.                 final int prime = 31;
  26.                 int result = 1;
  27.                 result = prime * result + age;
  28.                 result = prime * result + ((name == null) ? 0 : name.hashCode());
  29.                 return result;
  30.         }
  31.         @Override
  32.         public boolean equals(Object obj) {
  33.                 if (this == obj)
  34.                         return true;
  35.                 if (obj == null)
  36.                         return false;
  37.                 if (getClass() != obj.getClass())
  38.                         return false;
  39.                 Student other = (Student) obj;
  40.                 if (age != other.age)
  41.                         return false;
  42.                 if (name == null) {
  43.                         if (other.name != null)
  44.                                 return false;
  45.                 } else if (!name.equals(other.name))
  46.                         return false;
  47.                 return true;
  48.         }
  49.        
  50. }
复制代码



1 个回复

倒序浏览
输出:
添加对象:true
添加对象:true
添加对象:true
添加对象:true
添加对象:true
添加对象:false
Lili...17...hashCode:        2369962
Cici...15...hashCode:        2101502
Lili...15...hashCode:        2369900
Lucy...15...hashCode:        2381169
Lala...8...hashCode:        2361987
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马