1
+ // the is written by chatgpt , all operation . In The Problem we use the inbuild HashMap.
2
+
1
3
2
4
import java .util .LinkedList ;
3
5
@@ -13,25 +15,182 @@ public Node(K key, V value) {
13
15
}
14
16
}
15
17
16
- private int n ;
17
- private int N ;
18
+ private int n ;// n-> nodes
19
+ private int N ;// N-> buckets
18
20
private LinkedList <Node > buckets [];
19
21
20
22
//constructor of HashMap
23
+ @ SuppressWarnings ("unchecked" )
21
24
public HashMap (){
22
25
this .N = 4 ;
23
26
this .buckets = new LinkedList [4 ];
24
27
for (int i = 0 ; i < N ; i ++) {
25
28
this .buckets [i ] = new LinkedList <>();
26
29
}
27
30
}
28
- public void put (K key , V value ){
29
-
31
+ // Hash function to get bucket index
32
+ private int hashFunction (K key ) {
33
+ int hc = key .hashCode ();
34
+ return Math .abs (hc ) % N ;
35
+ }
36
+
37
+ // Search for a key in the bucket and return its index
38
+ private int searchInLL (K key , int bi ) {
39
+ LinkedList <Node > ll = buckets [bi ];
40
+ int di = 0 ;
41
+ for (int i = 0 ; i < ll .size (); i ++) {
42
+ Node node = ll .get (i );
43
+ if (node .key .equals (key )) {
44
+ return di ;
45
+ }
46
+ di ++;
47
+ }
48
+ return -1 ;
49
+ }
50
+
51
+ // Rehash the HashMap when load factor exceeds threshold
52
+ @ SuppressWarnings ("unchecked" )
53
+ private void rehash () {
54
+ LinkedList <Node > oldBuckets [] = buckets ;
55
+ buckets = new LinkedList [N * 2 ];
56
+ N = 2 * N ;
57
+ for (int i = 0 ; i < buckets .length ; i ++) {
58
+ buckets [i ] = new LinkedList <>();
59
+ }
60
+
61
+ // Transfer all nodes to new buckets
62
+ n = 0 ;
63
+ for (int i = 0 ; i < oldBuckets .length ; i ++) {
64
+ LinkedList <Node > ll = oldBuckets [i ];
65
+ for (int j = 0 ; j < ll .size (); j ++) {
66
+ Node node = ll .remove ();
67
+ put (node .key , node .value );
68
+ }
69
+ }
70
+ }
71
+
72
+ // Put operation - Insert or update key-value pair
73
+ public void put (K key , V value ) {
74
+ int bi = hashFunction (key );
75
+ int di = searchInLL (key , bi );
76
+
77
+ if (di != -1 ) {
78
+ // Key exists, update value
79
+ Node node = buckets [bi ].get (di );
80
+ node .value = value ;
81
+ } else {
82
+ // Key doesn't exist, add new node
83
+ buckets [bi ].add (new Node (key , value ));
84
+ n ++;
85
+ }
86
+
87
+ double lambda = (double ) n / N ;
88
+ if (lambda > 2.0 ) {
89
+ rehash ();
90
+ }
91
+ }
92
+
93
+ // Check if key exists in HashMap
94
+ public boolean containsKey (K key ) {
95
+ int bi = hashFunction (key );
96
+ int di = searchInLL (key , bi );
97
+ return di != -1 ;
98
+ }
99
+
100
+ // Get value for a given key
101
+ public V get (K key ) {
102
+ int bi = hashFunction (key );
103
+ int di = searchInLL (key , bi );
104
+
105
+ if (di != -1 ) {
106
+ Node node = buckets [bi ].get (di );
107
+ return node .value ;
108
+ } else {
109
+ return null ;
110
+ }
111
+ }
30
112
113
+ // Remove key-value pair from HashMap
114
+ public V remove (K key ) {
115
+ int bi = hashFunction (key );
116
+ int di = searchInLL (key , bi );
117
+
118
+ if (di != -1 ) {
119
+ Node node = buckets [bi ].remove (di );
120
+ n --;
121
+ return node .value ;
122
+ } else {
123
+ return null ;
124
+ }
31
125
}
32
126
33
- public boolean containsKey (K key ){
34
- return false ;
127
+ // Get all keys in the HashMap
128
+ public java .util .ArrayList <K > keySet () {
129
+ java .util .ArrayList <K > keys = new java .util .ArrayList <>();
130
+ for (int i = 0 ; i < buckets .length ; i ++) {
131
+ LinkedList <Node > ll = buckets [i ];
132
+ for (Node node : ll ) {
133
+ keys .add (node .key );
134
+ }
135
+ }
136
+ return keys ;
35
137
}
138
+
139
+ // Check if HashMap is empty
140
+ public boolean isEmpty () {
141
+ return n == 0 ;
142
+ }
143
+
144
+ // Get the size of HashMap
145
+ public int size () {
146
+ return n ;
147
+ }
148
+
149
+ // Print the HashMap for debugging
150
+ public void printHashMap () {
151
+ System .out .println ("HashMap contents:" );
152
+ for (int i = 0 ; i < buckets .length ; i ++) {
153
+ System .out .print ("Bucket " + i + ": " );
154
+ LinkedList <Node > ll = buckets [i ];
155
+ for (Node node : ll ) {
156
+ System .out .print ("(" + node .key + "," + node .value + ") " );
157
+ }
158
+ System .out .println ();
159
+ }
160
+ }
161
+ }
162
+
163
+ // Main method for testing
164
+ public static void main (String [] args ) {
165
+ HashMap <String , Integer > hm = new HashMap <>();
166
+
167
+ // Testing put operation
168
+ hm .put ("India" , 100 );
169
+ hm .put ("China" , 150 );
170
+ hm .put ("US" , 50 );
171
+ hm .put ("Nepal" , 5 );
172
+
173
+ System .out .println ("Size: " + hm .size ());
174
+ hm .printHashMap ();
175
+
176
+ // Testing get operation
177
+ System .out .println ("\n Get 'India': " + hm .get ("India" ));
178
+ System .out .println ("Get 'China': " + hm .get ("China" ));
179
+ System .out .println ("Get 'Pakistan': " + hm .get ("Pakistan" ));
180
+
181
+ // Testing containsKey operation
182
+ System .out .println ("\n Contains 'India': " + hm .containsKey ("India" ));
183
+ System .out .println ("Contains 'Pakistan': " + hm .containsKey ("Pakistan" ));
184
+
185
+ // Testing remove operation
186
+ System .out .println ("\n Removing 'China': " + hm .remove ("China" ));
187
+ System .out .println ("Size after removal: " + hm .size ());
188
+ hm .printHashMap ();
189
+
190
+ // Testing keySet operation
191
+ System .out .println ("\n All keys: " + hm .keySet ());
192
+
193
+ // Testing isEmpty
194
+ System .out .println ("Is empty: " + hm .isEmpty ());
36
195
}
37
196
}
0 commit comments