Skip to content

Commit d56136c

Browse files
committed
Containers in Depth: Exercise 26
1 parent c007c94 commit d56136c

File tree

2 files changed

+77
-2
lines changed

2 files changed

+77
-2
lines changed

ContainersInDepth/CountedString.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import java.util.List;
2+
import java.util.ArrayList;
3+
import java.util.Map;
4+
import java.util.HashMap;
5+
6+
public class CountedString {
7+
private static List<String> created = new ArrayList<String>();
8+
private String string;
9+
private int id = 0;
10+
private char aChar;
11+
12+
public CountedString(String string, char aChar) {
13+
this.string = string;
14+
this.aChar = aChar;
15+
created.add(string + aChar);
16+
for (String createdString : created) {
17+
if (createdString.equals(string + aChar)) {
18+
id++;
19+
}
20+
}
21+
}
22+
23+
public String toString() {
24+
return "String: " + string + " id: " + id + " char: " + aChar + " hashCode(): " + hashCode();
25+
}
26+
27+
public int hashCode() {
28+
int result = 17;
29+
result = 37 * result + string.hashCode();
30+
result = 37 * result + id;
31+
result = 37 * result + (int) aChar;
32+
return result;
33+
}
34+
35+
public boolean equals(Object object) {
36+
return object instanceof CountedString &&
37+
string.equals(((CountedString) object).string) &&
38+
id == ((CountedString) object).id &&
39+
aChar == ((CountedString) object).aChar;
40+
}
41+
42+
public static void main(String[] args) {
43+
Map<CountedString, Integer> map = new HashMap<CountedString, Integer>();
44+
CountedString[] countedStrings = new CountedString[5];
45+
for (int i = 0; i < countedStrings.length; ++i) {
46+
countedStrings[i] = new CountedString("hi", (char) ('a' + i));
47+
map.put(countedStrings[i], i);
48+
}
49+
Util.println(map);
50+
for (CountedString countedString : countedStrings) {
51+
Util.println("Looking up " + countedString);
52+
Util.println(map.get(countedString));
53+
}
54+
}
55+
}

ContainersInDepth/README.md

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,26 @@ This means that we should pay attention to whether override `entrySet()` or not
188188
Therefore, all of the methods have been implemented.
189189
Many methods depend on `entrySet()` which makes `entrySet()` important.
190190

191-
I spent most of today on work even after work time...
192-
191+
### Overriding hashCode()
192+
1. The most important factor in creating a `hashCode()` is that, regardless of when `hashCode()` is called,
193+
it produces the same value for a particular object every time.
194+
Otherwise, it will be impossible to access the stored ones.
195+
196+
2. Do **NOT** use unique object information, such as the address.
197+
Otherwise, it will be impossible again.
198+
For example,
199+
```java
200+
map.put(instance); // where instance.hashCode() is based on `this`
201+
Instance instance2 = instance;
202+
map.get(instance2); // this will return `null`
203+
```
204+
205+
3. Another factor needs to consider while overriding `hashCode()` is to ensure an even distribution,
206+
so that less collisions happen.
207+
According to ***Effective Java***, use `result = 37* result + c` where `c` is the `hashCode` of each field of the class.
208+
209+
**Note**:
210+
1. The access modifiers are controlling class access, not instance access.
211+
That's why in 'equals()', private fields can be accessed.
212+
[CountedString.java](https://github.com/kean0212/Thinking-In-Java-Notes/blob/master/ContainersInDepth/CountedString.java#37).
193213

0 commit comments

Comments
 (0)