-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathHashCode.java
46 lines (42 loc) · 1.35 KB
/
HashCode.java
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
package advancedClassDesign;
/**
*
* @author chengfeili
* Jun 3, 2017 1:30:00 PM
*
* Whenever you override equals(),
* you are also expected to override hashCode()
*
* 1. Within the same program, the result of hashCode() must not change.
* this means that you sholdn't include variables that change in figuring
* out the hash code.
*
* 2. If equals() returns true when called with two objects, calling
* hashCode() on each of those objects must return the same result.
* This means hashCode() can use a subset of the variables that equals uses.
*
* 3. If equals returns false when called with two objects, calling hashCode() on each of
* those objects does not have to return a different result. This means hashCode() results
* do not need to be unique when called on unequal objects.
*/
public class HashCode {
private String rank;
private String suit;
public HashCode(String r, String s) {
if (r == null || s == null) {
throw new IllegalArgumentException();
}
rank = r;
suit = s;
}
public boolean equals(Object obj) {
if (!(obj instanceof HashCode)) {
return false;
}
HashCode card = (HashCode) obj;
return rank.equals(card.rank) && suit.equals(card.suit);
}
public int hashCode() {
return rank.hashCode();
}
}