-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashMap_01.java
More file actions
167 lines (134 loc) · 3.93 KB
/
Copy pathHashMap_01.java
File metadata and controls
167 lines (134 loc) · 3.93 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Implementation Of HashMap In Java
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Map.Entry;
class HashMap_Implementation<K,V>{
private class Node{
K key;
V val;
public Node(K key,V val){
this.key=key;
this.val=val;
}
}
private int n; // No. of nodes in a Single Bucket (Linked List)
private int N; // No. of Buckets
private LinkedList<Node> Buckets[];
@SuppressWarnings("unchecked")
public HashMap_Implementation(){
this.N=9; // Let's suppose we have 9 Buckets
this.Buckets = new LinkedList[this.N];
for(int i=0;i<N;i++){
Buckets[i]=new LinkedList<>();
}
}
private int Hash_Function(K key){
int idx = key.hashCode();
return Math.abs(idx) % N;
}
private int Search_In_LL(K key , int Buck_idx){
LinkedList<Node> Bucket = Buckets[Buck_idx];
for(int i=0;i<Bucket.size();i++){
if(key==Bucket.get(i).key){
return i;
}
}
return -1;
}
@SuppressWarnings("unchecked")
private void Rehash(){
LinkedList<Node> Old_Buckets[] = Buckets;
Buckets = new LinkedList[N*2]; // Double The No. of Buckets
N = N * 2; // Update N
for(int i=0;i<N*2;i++){
Buckets[i] = new LinkedList<>();
}
n = 0; // Reset n, because put() will recalculate it
for(int i=0;i<Old_Buckets.length;i++){
LinkedList<Node> ll = Old_Buckets[i];
for(int j=0;j<ll.size();j++){
Node node = ll.get(j);
put(node.key, node.val);
}
}
}
public void put(K key,V val){
int Buck_idx = Hash_Function(key);
int key_idx = Search_In_LL(key,Buck_idx);
if(key_idx == -1){ // Key Doesn't Exist
Buckets[Buck_idx].add(new Node(key, val));
n++;
}
else{ //Key Already Exist
Node node = Buckets[Buck_idx].get(key_idx);
node.val=val;
}
double lambda = (double) n/N;
if(lambda > 2.0){
Rehash();
}
}
public boolean contains_Key(K key){
int Buck_idx=Hash_Function(key);
int key_idx=Search_In_LL(key, Buck_idx);
if(key_idx==-1){
return false;
}
else{
return true;
}
}
public V Remove(K key){
int Buck_idx=Hash_Function(key);
int key_idx=Search_In_LL(key, Buck_idx);
if(key_idx==-1){
return null;
}
else{
Node node = Buckets[Buck_idx].remove(key_idx);
n--;
return node.val;
}
}
public V get(K key){
int Buck_idx=Hash_Function(key);
int key_idx=Search_In_LL(key, Buck_idx);
if(key_idx==-1){
return null;
}
else{
Node node = Buckets[Buck_idx].get(key_idx);
return node.val;
}
}
public ArrayList<K> Key_Set(){
ArrayList<K> keys = new ArrayList<>();
for(int i=0;i<Buckets.length;i++){
LinkedList<Node> ll=Buckets[i];
for(int j=0;j<ll.size();j++){
Node node = ll.get(j);
keys.add(node.key);
}
}
return keys;
}
public boolean isEmpty(){
return n==0;
}
}
public class HashMap_01 {
public static void main(String[] args){
HashMap_Implementation<String,Integer> map = new HashMap_Implementation<>();
map.put("India", 50);
map.put("China", 50);
map.put("USA", 50);
map.put("Russia", 50);
map.put("India", 50);
ArrayList<String> keys = map.Key_Set();
for(int i=0;i<keys.size();i++){
System.out.println(keys.get(i) + " " + map.get(keys.get(i)));
}
map.Remove("India");
System.out.println(map.get("India"));
}
}