-
-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathDesignHashMap706.java
126 lines (108 loc) · 2.78 KB
/
DesignHashMap706.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
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
package easy;
import java.util.LinkedList;
public class DesignHashMap706 {
private class HTPair {
int key;
int value;
HTPair(int key, int value) {
this.key = key;
this.value = value;
}
}
private int size;
private static final int DEFAULT_CAPACITY = 10;
private LinkedList<HTPair>[] bucketarray;
/** Initialize your data structure here. */
public DesignHashMap706() {
initBucket(DEFAULT_CAPACITY);
size = 0;
}
private void initBucket(int N) {
bucketarray = new LinkedList[N];
for (int bi = 0; bi < bucketarray.length; bi++) {
bucketarray[bi] = new LinkedList<HTPair>();
}
}
/** value will always be non-negative. */
public void put(int key, int value) {
int bucketIndex = hashFunction(key);
int dataIndex = getDataWithinBucket(key, bucketIndex);
if (dataIndex == -1) {
// no index found
HTPair newPair = new HTPair(key, value);
bucketarray[bucketIndex].add(newPair);
size++;
} else {
LinkedList<HTPair> bucketLocation = bucketarray[bucketIndex];
bucketLocation.get(dataIndex).value = value;
}
// rehasing threshold
double lambda = size * 1.0 / bucketarray.length;
if (lambda > 2.0) {
rehash();
}
}
private void rehash() {
LinkedList<HTPair>[] oldbucket = bucketarray;
initBucket(oldbucket.length * 2);
size = 0;
for (int bi = 0; bi < oldbucket.length; bi++) {
for (HTPair pairs : oldbucket[bi]) {
put(pairs.key, pairs.value);
}
}
}
private int hashFunction(Integer key) {
int hashCode = key.hashCode();
return Math.abs(hashCode) % bucketarray.length;
}
private int getDataWithinBucket(int keys, int bi) {
int id = 0;
for (HTPair pairs : bucketarray[bi]) {
if (pairs.key == keys) // == here because we used int as key,value
return id;
id++;
}
return -1;
}
/**
* Returns the value to which the specified key is mapped, or -1 if this map
* contains no mapping for the key
*/
public int get(int key) {
int bucketIndex = hashFunction(key);
int dataIndex = getDataWithinBucket(key, bucketIndex);
if (dataIndex == -1) {
return dataIndex; // no index found
} else {
LinkedList<HTPair> bucketLocation = bucketarray[bucketIndex];
return bucketLocation.get(dataIndex).value;
}
}
/**
* Removes the mapping of the specified value key if this map contains a
* mapping for the key
*/
public void remove(int key) {
int bucketIndex = hashFunction(key);
int dataIndex = getDataWithinBucket(key, bucketIndex);
if (dataIndex == -1) {
return;
} else {
LinkedList<HTPair> bucketLocation = bucketarray[bucketIndex];
bucketLocation.remove(dataIndex);
size--;
}
}
}
/**
* Your MyHashMap object will be instantiated and called as such:
*
* MyHashMap obj = new MyHashMap();
*
* obj.put(key,value);
*
* int param_2 = obj.get(key);
*
* obj.remove(key);
*/