Skip to content

Commit 7783b32

Browse files
committed
LC#706 hashmap implementaion from scratch
1 parent 80c82ac commit 7783b32

File tree

1 file changed

+126
-0
lines changed

1 file changed

+126
-0
lines changed
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
package easy;
2+
3+
import java.util.LinkedList;
4+
5+
public class DesignHashMap706 {
6+
7+
private class HTPair {
8+
int key;
9+
int value;
10+
11+
HTPair(int key, int value) {
12+
this.key = key;
13+
this.value = value;
14+
}
15+
}
16+
17+
private int size;
18+
private static final int DEFAULT_CAPACITY = 10;
19+
private LinkedList<HTPair>[] bucketarray;
20+
21+
/** Initialize your data structure here. */
22+
public DesignHashMap706() {
23+
initBucket(DEFAULT_CAPACITY);
24+
size = 0;
25+
}
26+
27+
private void initBucket(int N) {
28+
bucketarray = new LinkedList[N];
29+
for (int bi = 0; bi < bucketarray.length; bi++) {
30+
bucketarray[bi] = new LinkedList<HTPair>();
31+
}
32+
}
33+
34+
/** value will always be non-negative. */
35+
public void put(int key, int value) {
36+
int bucketIndex = hashFunction(key);
37+
int dataIndex = getDataWithinBucket(key, bucketIndex);
38+
39+
if (dataIndex == -1) {
40+
// no index found
41+
HTPair newPair = new HTPair(key, value);
42+
bucketarray[bucketIndex].add(newPair);
43+
size++;
44+
} else {
45+
LinkedList<HTPair> bucketLocation = bucketarray[bucketIndex];
46+
bucketLocation.get(dataIndex).value = value;
47+
}
48+
49+
// rehasing threshold
50+
double lambda = size * 1.0 / bucketarray.length;
51+
if (lambda > 2.0) {
52+
rehash();
53+
}
54+
}
55+
56+
private void rehash() {
57+
LinkedList<HTPair>[] oldbucket = bucketarray;
58+
initBucket(oldbucket.length * 2);
59+
size = 0;
60+
for (int bi = 0; bi < oldbucket.length; bi++) {
61+
for (HTPair pairs : oldbucket[bi]) {
62+
put(pairs.key, pairs.value);
63+
}
64+
}
65+
}
66+
67+
private int hashFunction(Integer key) {
68+
int hashCode = key.hashCode();
69+
return Math.abs(hashCode) % bucketarray.length;
70+
}
71+
72+
private int getDataWithinBucket(int keys, int bi) {
73+
int id = 0;
74+
for (HTPair pairs : bucketarray[bi]) {
75+
if (pairs.key == keys) // == here because we used int as key,value
76+
return id;
77+
id++;
78+
}
79+
return -1;
80+
}
81+
82+
/**
83+
* Returns the value to which the specified key is mapped, or -1 if this map
84+
* contains no mapping for the key
85+
*/
86+
public int get(int key) {
87+
int bucketIndex = hashFunction(key);
88+
int dataIndex = getDataWithinBucket(key, bucketIndex);
89+
90+
if (dataIndex == -1) {
91+
return dataIndex; // no index found
92+
} else {
93+
LinkedList<HTPair> bucketLocation = bucketarray[bucketIndex];
94+
return bucketLocation.get(dataIndex).value;
95+
}
96+
}
97+
98+
/**
99+
* Removes the mapping of the specified value key if this map contains a
100+
* mapping for the key
101+
*/
102+
public void remove(int key) {
103+
int bucketIndex = hashFunction(key);
104+
int dataIndex = getDataWithinBucket(key, bucketIndex);
105+
106+
if (dataIndex == -1) {
107+
return;
108+
} else {
109+
LinkedList<HTPair> bucketLocation = bucketarray[bucketIndex];
110+
bucketLocation.remove(dataIndex);
111+
size--;
112+
}
113+
}
114+
}
115+
116+
/**
117+
* Your MyHashMap object will be instantiated and called as such:
118+
*
119+
* MyHashMap obj = new MyHashMap();
120+
*
121+
* obj.put(key,value);
122+
*
123+
* int param_2 = obj.get(key);
124+
*
125+
* obj.remove(key);
126+
*/

0 commit comments

Comments
Β (0)