-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathMyHashMap.java
More file actions
58 lines (49 loc) · 1.15 KB
/
Copy pathMyHashMap.java
File metadata and controls
58 lines (49 loc) · 1.15 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
package com.allendowney.thinkdast;
/**
*
*/
import java.util.List;
import java.util.Map;
/**
* Implementation of a HashMap using a collection of MyLinearMap and
* resizing when there are too many entries.
*
* @author downey
* @param <K>
* @param <V>
*
*/
public class MyHashMap<K, V> extends MyBetterMap<K, V> implements Map<K, V> {
// average number of entries per map before we rehash
protected static final double FACTOR = 1.0;
@Override
public V put(K key, V value) {
V oldValue = super.put(key, value);
//System.out.println("Put " + key + " in " + map + " size now " + map.size());
// check if the number of elements per map exceeds the threshold
if (size() > maps.size() * FACTOR) {
rehash();
}
return oldValue;
}
/**
* Doubles the number of maps and rehashes the existing entries.
*/
/**
*
*/
protected void rehash() {
// TODO: FILL THIS IN!
}
/**
* @param args
*/
public static void main(String[] args) {
Map<String, Integer> map = new MyHashMap<String, Integer>();
for (int i=0; i<10; i++) {
map.put(new Integer(i).toString(), i);
}
Integer value = map.get("3");
System.out.println(value);
}
}