forked from super30admin/Design-1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMy_Linked_Hash_Map.java
More file actions
101 lines (93 loc) · 3.11 KB
/
My_Linked_Hash_Map.java
File metadata and controls
101 lines (93 loc) · 3.11 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
// Time Complexity : get O(1)
// put O(1)
// remove O(1)
// Space Complexity : O(N)
// Run on leetcode : YES
// Hashmap with Linkedlist implementation
// public class My_Linked_Hash_Map{
public class My_Linked_Hash_Map{
class Node{
int key;
int value;
Node prev;
Node next;
Node(int key,int value){
this.key = key;
this.value = value;
}
}
Node[] nodes;
public My_Linked_Hash_Map(){
nodes = new Node[10000];
}
public int index(int key){
return Integer.hashCode(key) % nodes.length;
}
private Node find(Node head, int key){
Node curr = head;
Node prev = null;
while( curr != null && curr.key != key){
prev = curr;
curr = curr.next;
}
return prev;
}
/** value will always be non-negative. */
public void put(int key, int value) {
int index = index(key);
// if there is null value at index then create new linkedlist
if(this.nodes[index] == null){
// initialize the empty linkedlist with New Node having key and value = -1
this.nodes[index] =new Node(-1,-1);
}
// find the location where we should put new key to hashmap
Node prev = find(nodes[index], key);
// if key is not present then add it at the end of linkedlist
if(prev.next == null){
prev.next = new Node(key, value);
}else{
// otherwise update the value
prev.next.value = value;
}
}
/** 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) {
// get the index of bucket
int index = index(key);
// check the location of nodes
if(this.nodes[index] == null){
return -1;
}
Node prev = find(nodes[index], key);
if(prev.next == null){
return -1;
}else{
return prev.next.value;
}
}
/** Removes the mapping of the specified value key if this map contains a mapping for the key */
public void remove(int key) {
// get the index of bucket
int index = index(key);
// check the location of the node to be removed
if(nodes[index] == null) return;
Node prev = find(nodes[index], key);
// if node is null return -1 else return by removing the reference
if(prev.next == null){
return;
}else{
prev.next = prev.next.next;
}
}
// public static void main(String[] args) {
// My_Linked_Hash_Map map = new My_Linked_Hash_Map();
// map.put(1,1);
// map.put(2,2);
// System.out.println(map.get(1));
// System.out.println(map.get(3));
// map.put(2,1);
// System.out.println(map.get(2));
// map.remove(2);
// System.out.println(map.get(2));
// }
}