forked from super30admin/Design-2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExercise_2.java
More file actions
126 lines (111 loc) · 3.72 KB
/
Exercise_2.java
File metadata and controls
126 lines (111 loc) · 3.72 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
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
//We are going to use Linear hashing to check for collisions using Linked List
//the LinkedList node array is instantiated with 10^4 length to reduce the size of the inner linked list
//to avoid traversing a lot
//TC: O(1) because there can only be 100 elements in the inner linked list
//SC: O(1)
public class MyHashMap {
private final Node[] storage;
private final int buckets;
/**
* Linked List definition
*/
public static class Node {
int key;
int value;
Node next;
/**
* Constructor for linked list
* @param key the key to store
* @param value the value for the key
*/
public Node(int key, int value) {
this.key = key;
this.value = value;
this.next = null;
}
}
/**
* Initializing constructor to set up the buckets and the storage
*/
public MyHashMap() {
this.buckets = 10000;
this.storage = new Node[buckets];
}
/**
*
* @param key the key to store in the map
* @return Calculates and returns the hash key for the node array using modulo
*/
private int hash(int key) {
return key % buckets;
}
/**
* The key value pair to store in the map
* @param key the key to store in the map
* @param value the value for the key
*/
public void put(int key, int value) {
final int hash = hash(key);
if (this.storage[hash] == null) {
this.storage[hash] = new Node(-1, -1);
Node root = this.storage[hash];
root.next = new Node(key, value);
} else {
final Node prev = helper(this.storage[hash], key);
//either we get the key in the linked list or we don't have current key in the list
if (prev.next == null) {
prev.next = new Node(key, value);
} else {
prev.next.value = value;
}
}
}
private Node helper(Node head, int key) {
Node prev = head;
Node current = prev.next;
while (current != null && current.key != key) {
prev = current;
current = current.next;
}
return prev;
}
/**
*
* @param key the key to search for
* @return the value corresponding to the key, if not present return -1
*/
public int get(int key) {
final int hash = hash(key);
if (this.storage[hash] == null) return -1;
final Node prev = helper(this.storage[hash], key);
if (prev.next == null) {
return -1;
} else {
return prev.next.value;
}
}
/**
* @param key the key to remove
*/
public void remove(int key) {
int hash = hash(key);
if (this.storage[hash] == null) return;
final Node prev = helper(this.storage[hash], key);
if (prev.next != null) {
Node temp = prev.next;
prev.next = temp.next;
temp.next = null;
}
}
public static void main(String[] args) {
final MyHashMap myHashMap = new MyHashMap();
myHashMap.put(1, 1); // The map is now [[1,1]]
myHashMap.put(2, 2); // The map is now [[1,1], [2,2]]
myHashMap.get(1); // return 1, The map is now [[1,1], [2,2]]
myHashMap.get(3); // return -1 (i.e., not found), The map is now [[1,1], [2,2]]
myHashMap.put(2, 1); // The map is now [[1,1], [2,1]] (i.e., update the existing value)
myHashMap.get(2); // return 1, The map is now [[1,1], [2,1]]
myHashMap.remove(2); // remove the mapping for 2, The map is now [[1,1]]
myHashMap.get(2); // return -1 (i.e., not found), The map is now [[1,1]]
}
}