Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathHashWithCustomizedClass(LinkedList).java
More file actions
Latest commit
executable file
·175 lines (142 loc) · 5.22 KB
/
Copy pathHashWithCustomizedClass(LinkedList).java
File metadata and controls
executable file
·175 lines (142 loc) · 5.22 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
M
tags: HashTable
练习HashMapwithcustomizedclass. functions: get(), put(), getRandom()
#### HashTable
- storemapasarray: `Entry<K,V>[] table;`
- storeentryaslinkedlist: `publicEntry(Kkey, Vvalue, Entry<K,V> next)`
- computehashKey: `Math.abs(key.hashCode()) % this.capacity`
- Handlecollision:
- 1.Checkifduplicate (matchingkey), ifso, replaceandreturn
- 2.Checkthroughthelinkedlist, findfindduplicate (matchingkey), replaceandreturn.
- 3.Ifnoduplicate, addtheentrytothetail
- Finditem: computehashKey -> findlinkedlist -> iterateoverlisttofindamatchingkey.
```
/*
Build Hashtable datastructure with three methods: set, get, getrandom.
Note1: getrandom needs to pick evenly/uniformaly distributed entry.
Note2: to be realistic, hashtable should be able to handle collision, that's where linkedlist comes into play.
Note3: use default object.hashcode() function to get hash code, then apply hashcode % table size
More about O(1) random access
If no collision, uniform random access is easy.
With collision, need to figure our how to access element on the linkedlist with O(1), but it's unlikely.
So, like Java HashMap, we can implement rehashing.
Bascially, when map size increase to over capacity, double the capacity.
*/
importjava.io.*;
importjava.util.*;
publicclassCHashMap<K, V> {
publicintcapacity;
publicintcount;
publicEntry<K,V>[] table;
publicclassEntry<K, V> {
publicVvalue;
publicKkey;
publicEntry<K,V> next;
publicEntry(Kkey, Vvalue, Entry<K,V> next) {
this.value = value;
this.key = key;
this.next = next;
}
}
publicCHashMap() {
this.capacity = 16;
this.table = newEntry[this.capacity];
this.count = 0;
}
publicCHashMap(intcapacity) {
this.capacity = capacity;
this.table = newEntry[this.capacity];
this.count = 0;
}
publicVget(Kkey) {
if (key == null) {
returnnull;
}
inthashedKey = hashFunction(key);
if (table[hashedKey] != null) {
Entry<K,V> node = table[hashedKey];
while (node != null) {
if (node.key.equals(key)) {
returnnode.value;
}
node = node.next;
}
}
returnnull;
}
publicvoidput(Kkey, Vvalue) {
if (key == null) {
return;
}
this.count++;
Entry<K,V> entry = newEntry<K,V>(key, value, null);
inthashedKey = hashFunction(key);
if (table[hashedKey] == null) {
table[hashedKey] = entry;
} else {
// Handle collision
Entry<K,V> node = table[hashedKey];
if (node.key.equals(key)) { // replace entry if key matches
table[hashedKey] = entry;
entry.next = node.next;
return;
}
while (node.next != null) {
if (node.next.key.equals(key)) { // replace entry if key matches
Entry<K,V> temp = node.next;
node.next = entry;
entry.next = temp.next;
return;
}
node = node.next;
}
node.next = entry;
}
}
publicinthashFunction (Kkey) {
returnMath.abs(key.hashCode()) % this.capacity;
}
publicvoiddisplay() {
for (inti = 0; i < table.length; i++) {
Entry<K,V> node = table[i];
StringBuffersb = newStringBuffer();
while (node != null) {
sb.append("[key: " + node.key + ", value: " + node.value + "], ");
node = node.next;
}
if (sb.length() != 0)
System.out.println(sb.toString());
}
System.out.println();
}
/*
If no collision, uniform random access is easy.
With collision, need to figure our how to access element on the linkedlist with O(1), but it's unlikely.
*/
publicVgetRandom() {
Randomrd = newRandom();
inthashedKey = hashFunction(rd.nextInt(this.capacity));
returntable[hashedKey];
}
publicstaticvoidmain(String[] args) {
CHashMap<Character, String> map = newCHashMap<Character, String>(2);
System.out.println("TEST SET------");
map.put('a', "1st string");
map.put('b', "2nd string!");
map.display();
map.put('a', "wowo change");
map.display();
System.out.println("TEST PUT------");
System.out.println(map.get('a'));
System.out.println(map.get('c'));
map.put('c', "okay test c");
System.out.println(map.get('c'));
map.display();
System.out.println("TEST COLLISION------");
map.put('d', "test d");
map.put('e', "test E");
map.display();
//test getrandom
}
}
```