-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathStudent.java
More file actions
52 lines (37 loc) · 1.23 KB
/
Student.java
File metadata and controls
52 lines (37 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package hashcode_test;
public class Student {
int grade;
int cls;
String firstName;
String lastName;
Student(int grade, int cls, String firstName, String lastName) {
this.grade = grade;
this.cls = cls;
this.firstName = firstName;
this.lastName = lastName;
}
public int hashCode() {
int B = 31;
int hash = 0;
hash = hash * B + grade;
hash = hash * B + cls;
hash = hash * B + firstName.toLowerCase().hashCode(); // 忽略大小写
hash = hash * B + lastName.toLowerCase().hashCode(); // 忽略大小写
// 可能产生溢出,但依然没有关系
return hash;
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null)
return false;
if (!(o instanceof Student) || getClass() != o.getClass())
return false;
Student another = (Student) o;
return this.grade == another.grade &&
this.cls == another.cls &&
this.firstName.toLowerCase().equals(another.firstName.toLowerCase()) &&
this.lastName.toLowerCase().equals(another.lastName.toLowerCase());
}
}