Skip to content

Commit a184cef

Browse files
Add files via upload
1 parent b97df27 commit a184cef

File tree

2 files changed

+166
-0
lines changed

2 files changed

+166
-0
lines changed
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
package map;
2+
3+
import java.util.ArrayList;
4+
5+
public class MyHashMap<K, V> {
6+
7+
ArrayList<MapNode<K, V>> buckets;
8+
int size; // number of entries
9+
int numBuckets; // number of buckets
10+
11+
public MyHashMap() {
12+
numBuckets = 5;
13+
buckets = new ArrayList<>();
14+
for (int i = 0; i < numBuckets; i++) {
15+
buckets.add(null);
16+
}
17+
}
18+
19+
private int getBucketIndex(K key) {
20+
int hashCode = key.hashCode();
21+
return hashCode % numBuckets;
22+
}
23+
24+
public int size() {
25+
return size;
26+
}
27+
28+
public double loadFactor() {
29+
return (double) size / numBuckets;
30+
}
31+
32+
private void rehash() {
33+
System.out.println("\nNumber of Entries = " + size + "\nNumber of Buckets = " + numBuckets + "\nLoad Factor = "
34+
+ (double) size / numBuckets);
35+
ArrayList<MapNode<K, V>> temp = buckets;
36+
buckets = new ArrayList<>();
37+
for (int i = 0; i < 2 * numBuckets; i++) {
38+
buckets.add(null);
39+
}
40+
size = 0;
41+
numBuckets *= 2;
42+
for (int i = 0; i < temp.size(); i++) {
43+
MapNode<K, V> curr = temp.get(i);
44+
while (curr != null) {
45+
K key = curr.key;
46+
V value = curr.value;
47+
insert(key, value);
48+
curr = curr.next;
49+
}
50+
}
51+
}
52+
53+
public void insert(K key, V value) {
54+
int bucketIndex = getBucketIndex(key);
55+
MapNode<K, V> curr = buckets.get(bucketIndex); // curr is a pointer which will be used to traverse the
56+
// LinkedList
57+
58+
// traverse through the LinkedList, if the key is found then update it's value
59+
while (curr != null) {
60+
if (curr.key.equals(key)) {
61+
curr.value = value;
62+
return;
63+
}
64+
curr = curr.next;
65+
}
66+
// if the control comes here, this means that the key is not present in the Map,
67+
// so we will create a new MapNode
68+
curr = buckets.get(bucketIndex);
69+
MapNode<K, V> newElementNode = new MapNode<K, V>(key, value);
70+
newElementNode.next = curr;
71+
buckets.set(bucketIndex, newElementNode);
72+
size++;
73+
74+
double loadFactor = (double) size / numBuckets;
75+
if (loadFactor > 0.7)
76+
rehash();
77+
}
78+
79+
public boolean containsKey(K key) {
80+
int index = getBucketIndex(key);
81+
MapNode<K, V> curr = buckets.get(index); // this is a pointer which will traverse
82+
while (curr != null) {
83+
// here equals method can be override if you want to use your using your own
84+
// class object else Wrapper class like INTEGER, DOUBLE..have their own equals()
85+
if (curr.key.equals(key)) {
86+
return true;
87+
}
88+
curr = curr.next;
89+
}
90+
return false;
91+
}
92+
93+
public V getValue(K key) {
94+
int bucketIndex = getBucketIndex(key);
95+
MapNode<K, V> curr = buckets.get(bucketIndex); // curr is a pointer which will be used to traverse the
96+
// LinkedList
97+
98+
// traverse through the LinkedList, if the key is found then return its value
99+
while (curr != null) {
100+
if (curr.key.equals(key)) {
101+
return curr.value;
102+
}
103+
curr = curr.next;
104+
}
105+
return null;
106+
}
107+
108+
public V remove(K key) {
109+
int bucketIndex = getBucketIndex(key);
110+
// curr is a pointer which will be used to traverse the LinkedList
111+
MapNode<K, V> curr = buckets.get(bucketIndex);
112+
// prev is another pointer which will be used to store the location of the
113+
// previous node
114+
MapNode<K, V> prev = null;
115+
116+
// traverse through the LinkedList, if the key is found then return its value
117+
while (curr != null) {
118+
if (curr.key.equals(key)) {
119+
if (prev == null)
120+
buckets.set(bucketIndex, curr.next);
121+
else
122+
prev.next = curr.next;
123+
124+
size--;
125+
return curr.value;
126+
}
127+
prev = curr;
128+
curr = curr.next;
129+
}
130+
return null;
131+
}
132+
133+
public static void main(String[] args) {
134+
MyHashMap<String, Integer> map = new MyHashMap<>();
135+
for (int i = 1; i <= 20; i++) {
136+
map.insert("abc" + i, i);
137+
System.out.println("For Entry " + i + " LoadFactor = " + map.loadFactor());
138+
}
139+
System.out.println(map.containsKey("abc5"));
140+
map.remove("abc8");
141+
map.remove("abc9");
142+
map.remove("abc8");
143+
144+
for (int i = 0; i < map.size(); i++) {
145+
System.out.println("abc" + i + ":" + map.getValue("abc" + i));
146+
}
147+
}
148+
149+
}
150+
151+
class MapNode<K, V> {
152+
153+
K key;
154+
V value;
155+
MapNode<K, V> next;
156+
157+
public MapNode(K key, V value) {
158+
this.key = key;
159+
this.value = value;
160+
}
161+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
## Prerequisite: Learn about Sets in Java
2+
3+
## **STEP 1 : [Complete this awesome playlist by CodingNinja.](https://youtube.com/playlist?list=PLGeDISzAH2BQ59_Cft8hC-HAGbwv8dgQu)**
4+
5+
## **STEP 2 : [Visualize my code here ](./MyHashMap.java)**

0 commit comments

Comments
 (0)