質問編集履歴

1

該当するコードを追加しました。

2018/08/06 05:45

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -17,3 +17,251 @@
17
17
  注意:詳細は、-Xlint:uncheckedオプションを指定して再コンパイルしてください。
18
18
 
19
19
  ```
20
+
21
+
22
+
23
+ ### HashTable
24
+
25
+ ```Java
26
+
27
+ import java.util.List;
28
+
29
+ import java.util.LinkedList;
30
+
31
+ import java.util.Arrays;
32
+
33
+
34
+
35
+
36
+
37
+ public class HashTable<K, V> {
38
+
39
+
40
+
41
+ private Object[] entries;
42
+
43
+
44
+
45
+ private List<HashTableEntry<K, V>> bucket(int i) {
46
+
47
+ return (List<HashTableEntry<K, V>>)entries[i];
48
+
49
+ }
50
+
51
+
52
+
53
+ public HashTable(int numBuckets) {
54
+
55
+ if (numBuckets < 1)
56
+
57
+ throw new IllegalArgumentException("hash table must have at least one bucket");
58
+
59
+ this.entries = new Object[numBuckets];
60
+
61
+ for (int i = 0; i < numBuckets; i++)
62
+
63
+ this.entries[i] = new LinkedList<HashTableEntry<K, V>>();
64
+
65
+ }
66
+
67
+
68
+
69
+ public void put(K key, V value) {
70
+
71
+ HashTableEntry<K, V> e = new HashTableEntry<K, V>(key, value);
72
+
73
+ int h = e.getHashCode();
74
+
75
+ int b = Math.abs(h % this.entries.length);
76
+
77
+ bucket(b).add(e);
78
+
79
+ }
80
+
81
+
82
+
83
+ public V get(K key) {
84
+
85
+ int h = key.hashCode();
86
+
87
+ int b = Math.abs(h % this.entries.length);
88
+
89
+ for (HashTableEntry<K, V> e : bucket(b))
90
+
91
+ if (key.equals(e.getKey()))
92
+
93
+ return e.getValue();
94
+
95
+ return null;
96
+
97
+ }
98
+
99
+
100
+
101
+ public boolean includesKey(K key) {
102
+
103
+ int h = key.hashCode();
104
+
105
+ int b = Math.abs(h % this.entries.length);
106
+
107
+ for (HashTableEntry<K, V> e : bucket(b))
108
+
109
+ if (key.equals(e.getKey()))
110
+
111
+ return true;
112
+
113
+ return false;
114
+
115
+ }
116
+
117
+
118
+
119
+ public boolean includesValue(V value) {
120
+
121
+ int h = value.hashCode();
122
+
123
+ int b = Math.abs(h % this.entries.length);
124
+
125
+ for (HashTableEntry<K, V> e : bucket(b))
126
+
127
+ if (value.equals(e.getValue()))
128
+
129
+ return true;
130
+
131
+ return false;
132
+
133
+ }
134
+
135
+
136
+
137
+ public List<K> keys() {
138
+
139
+ List<K> klist = new LinkedList<K>();
140
+
141
+ for (int i = 0; i < this.entries.length; i++) {
142
+
143
+ klist = (List<K>)entries[i];
144
+
145
+ }
146
+
147
+ return klist;
148
+
149
+ }
150
+
151
+
152
+
153
+ public List<V> values() {
154
+
155
+ List<V> vlist = new LinkedList<V>();
156
+
157
+ for (int i = 0; i < this.entries.length; i++) {
158
+
159
+ vlist = (List<V>)entries[i];
160
+
161
+ }
162
+
163
+ return vlist;
164
+
165
+ }
166
+
167
+
168
+
169
+ public double loadFactor() {
170
+
171
+ double sum = 0;
172
+
173
+ for (int i = 0; i < this.entries.length; i++) {
174
+
175
+ sum += bucket(i).size();
176
+
177
+ }
178
+
179
+ return sum / this.entries.length;
180
+
181
+ }
182
+
183
+
184
+
185
+ public int numCollisions() {
186
+
187
+ int col = 0;
188
+
189
+ for (int i = 0; i < this.entries.length; i++) {
190
+
191
+ if (bucket(i).size() > 1)
192
+
193
+ col++;
194
+
195
+ }
196
+
197
+ return col;
198
+
199
+ }
200
+
201
+
202
+
203
+ public int numEmptyBuckets() {
204
+
205
+ int emp = 0;
206
+
207
+ for (int i = 0; i < this.entries.length; i++) {
208
+
209
+ if (bucket(i).isEmpty() == true)
210
+
211
+ emp++;
212
+
213
+ }
214
+
215
+ return emp;
216
+
217
+ }
218
+
219
+
220
+
221
+ public int maxBucket() {
222
+
223
+ int max = 0;
224
+
225
+ for (int i = 0; i < this.entries.length; i++) {
226
+
227
+ if (bucket(i).size() < bucket(i + 1).size()) {
228
+
229
+ max = bucket(i + 1).size();
230
+
231
+ } else {
232
+
233
+ max = bucket(i).size();
234
+
235
+ }
236
+
237
+ }
238
+
239
+ return max;
240
+
241
+ }
242
+
243
+
244
+
245
+ public static void main(String[] args) {
246
+
247
+ HashTable<String, Integer> tbl = new HashTable<String, Integer>(10);
248
+
249
+ tbl.put("A", 1);
250
+
251
+ tbl.put("C", 11);
252
+
253
+ tbl.put("B", 2);
254
+
255
+ System.out.println("A:" + tbl.get("A"));
256
+
257
+ System.out.println("C:" + tbl.get("C"));
258
+
259
+ System.out.println("B:" + tbl.get("B"));
260
+
261
+ }
262
+
263
+
264
+
265
+ }
266
+
267
+ ```