1. HashSet存储自定义对象并遍历
2. 代码示例:
(1)Student类,如下:
1 package cn.itcast_02;
2
3 /**
4 * @author Administrator
5 *
6 */
7 public class Student {
8 private String name;
9 private int age;
10
11 public Student() {
12 super();
13 }
14
15 public Student(String name, int age) {
16 super();
17 this.name = name;
18 this.age = age;
19 }
20
21 public String getName() {
22 return name;
23 }
24
25 public void setName(String name) {
26 this.name = name;
27 }
28
29 public int getAge() {
30 return age;
31 }
32
33 public void setAge(int age) {
34 this.age = age;
35 }
36
37
(2)测试类HashSetDemo2:
1 package cn.itcast_02;
2
3 import java.util.HashSet;
4
5 /*
6 存储自定义对象,并保证元素的唯一性
7 * 要求:如果两个对象的成员变量值都相同,则为同一个元素。
8 *
9 * 目前是不符合我的要求的:因为我们知道HashSet底层依赖的是hashCode()和equals()方法。
10 * 而这两个方法我们在学生类中没有重写,所以,默认使用的是Object类。
11 * 这个时候,他们的哈希值是不会一样的,根本就不会继续判断,执行了添加操作。
12 */
13 public class HashSetDemo2 {
14 public static void main(String[] args) {
15 // 创建集合对象
16 HashSet<Student> hs = new HashSet<Student>();
17
18 // 创建学生对象
19 Student s1 = new Student("林青霞", 27);
20 Student s2 = new Student("柳岩", 22);
21 Student s3 = new Student("王祖贤", 30);
22 Student s4 = new Student("林青霞", 27);
23 Student s5 = new Student("林青霞", 20);
24 Student s6 = new Student("范冰冰", 22);
25
26 // 添加元素
27 hs.add(s1);
28 hs.add(s2);
29 hs.add(s3);
30 hs.add(s4);
31 hs.add(s5);
32 hs.add(s6);
33
34 // 遍历集合
35 for (Student s : hs) {
36 System.out.println(s.getName() + "---" + s.getAge());
37 }
38 }
39
运行效果如下:
目前是不符合我的要求的:因为我们知道HashSet底层依赖的是hashCode()和equals()方法。 而这两个方法我们在学生类中没有重写,所以,默认使用的是Object类。这个时候,他们的哈希值是不会一样的,根本就不会继续判断,执行了添加操作。
3. 代码示例(修正)
(1)Student类:
1 package cn.itcast_02;
2
3 /**
4 * @author Administrator
5 *
6 */
7 public class Student {
8 private String name;
9 private int age;
10
11 public Student() {
12 super();
13 }
14
15 public Student(String name, int age) {
16 super();
17 this.name = name;
18 this.age = age;
19 }
20
21 public String getName() {
22 return name;
23 }
24
25 public void setName(String name) {
26 this.name = name;
27 }
28
29 public int getAge() {
30 return age;
31 }
32
33 public void setAge(int age) {
34 this.age = age;
35 }
36
37 @Override
38 public int hashCode() {
39 final int prime = 31;
40 int result = 1;
41 result = prime * result + age;
42 result = prime * result + ((name == null) ? 0 : name.hashCode());
43 return result;
44 }
45
46 @Override
47 public boolean equals(Object obj) {
48 if (this == obj)
49 return true;
50 if (obj == null)
51 return false;
52 if (getClass() != obj.getClass())
53 return false;
54 Student other = (Student) obj;
55 if (age != other.age)
56 return false;
57 if (name == null) {
58 if (other.name != null)
59 return false;
60 } else if (!name.equals(other.name))
61 return false;
62 return true;
63 }
64
65 // @Override
66 // public int hashCode() {
67 // // return 0;
68 // // 因为成员变量值影响了哈希值,所以我们把成员变量值相加即可
69 // // return this.name.hashCode() + this.age;
70 // // 看下面
71 // // s1:name.hashCode()=40,age=30
72 // // s2:name.hashCode()=20,age=50
73 // // 尽可能的区分,我们可以把它们乘以一些整数
74 // return this.name.hashCode() + this.age * 15;
75 // }
76 //
77 // @Override
78 // public boolean equals(Object obj) {
79 // // System.out.println(this + "---" + obj);
80 // if (this == obj) {
81 // return true;
82 // }
83 //
84 // if (!(obj instanceof Student)) {
85 // return false;
86 // }
87 //
88 // Student s = (Student) obj;
89 // return this.name.equals(s.name) && this.age == s.age;
90 // }
91 //
92 // @Override
93 // public String toString() {
94 // return "Student [name=" + name + ", age=" + age + "]";
95 // }
96
97
上面的hashCode 和 equals方法 都是使用Eclipse自动生成的
(2)测试类HashSetDemo2:
1 package cn.itcast_02;
2
3 import java.util.HashSet;
4
5 /*
6 * 需求:存储自定义对象,并保证元素的唯一性
7 * 要求:如果两个对象的成员变量值都相同,则为同一个元素。
8 *
9 * 目前是不符合我的要求的:因为我们知道HashSet底层依赖的是hashCode()和equals()方法。
10 * 而这两个方法我们在学生类中没有重写,所以,默认使用的是Object类。
11 * 这个时候,他们的哈希值是不会一样的,根本就不会继续判断,执行了添加操作。
12 */
13 public class HashSetDemo2 {
14 public static void main(String[] args) {
15 // 创建集合对象
16 HashSet<Student> hs = new HashSet<Student>();
17
18 // 创建学生对象
19 Student s1 = new Student("林青霞", 27);
20 Student s2 = new Student("柳岩", 22);
21 Student s3 = new Student("王祖贤", 30);
22 Student s4 = new Student("林青霞", 27);
23 Student s5 = new Student("林青霞", 20);
24 Student s6 = new Student("范冰冰", 22);
25
26 // 添加元素
27 hs.add(s1);
28 hs.add(s2);
29 hs.add(s3);
30 hs.add(s4);
31 hs.add(s5);
32 hs.add(s6);
33
34 // 遍历集合
35 for (Student s : hs) {
36 System.out.println(s.getName() + "---" + s.getAge());
37 }
38 }
39
运行效果如下: