forked from super30admin/Design-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sample.java
110 lines (93 loc) · 3.61 KB
/
Sample.java
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
// Time Complexity :
// Space Complexity :
// Did this code successfully run on Leetcode :
// Any problem you faced while coding this :
// Time complexity is O(1)
public class HashMap {
private static class Node{
int key, value;
Node next;
Node(int key,int value){
this.key = key;
this.value= value;
this.next = null;
}
}
private static final int DEFAULT_CAPACITY = 10000; // Capacity set to 10,000
private Node[] table;// Array to store the linked ;list
//Constructor
public HashMap(){
this.table = new Node[DEFAULT_CAPACITY];
// initialize each bucket with a dummy node
for (int i =0; i<DEFAULT_CAPACITY; i++){
table[i] = new Node(-1,-1); // dummy node with key -1 and value -1
}
}
// hash function to determine the index of an array
private int hash(int key){
return key% DEFAULT_CAPACITY;
}
// Function to find previous node of given key in linked list at a specific bucket
private Node getPrev(int key, int index) {
Node prev = table[index]; // get dummy head of the bucket
while (prev.next != null && prev.next.key != key) {
prev = prev.next;
}
return prev;
}
//insert a key value pair
public void put(int key, int value) {
int index = hash(key); // get bucket index
Node prev = getPrev(key, index);
// check if the key already exixts
if (prev.next == null) {
// key is not found , insert
prev.next = new Node(key, value);
} else {
prev.next.value = value;
}
}
public int get(int key) {
int index = hash(key);
Node prev = getPrev(key, index);
if (prev.next != null) {
return prev.next.value;
}
return -1;
}
public void remove(int key) {
int index = hash(key);
Node prev = getPrev(key, index);
if (prev.next != null) { // if key exist, remove the node by skipping it
prev.next = prev.next.next;
}
}
// Main method to test MyHashMap with the given constraints
public static void main(String[] args) {
HashMap hashMap = new HashMap();
// Testing with key and value range constraints
hashMap.put(1, 100);
hashMap.put(1000000, 500);
hashMap.put(500, 999);
// Get values for keys within the constraints
System.out.println("Value for key 1: " + hashMap.get(1)); // Should print 100
System.out.println("Value for key 1000000: " + hashMap.get(1000000)); // Should print 500
System.out.println("Value for key 500: " + hashMap.get(500)); // Should print 999
// Update the value for key 500
hashMap.put(500, 888);
System.out.println("Updated value for key 500: " + hashMap.get(500)); // Should print 888
// Remove key 1 and try getting its value
hashMap.remove(1);
System.out.println("Value for key 1 after removal: " + hashMap.get(1)); // Should print -1
// Check handling of a non-existing key
System.out.println("Value for key 12345: " + hashMap.get(12345)); // Should print -1
// Insert and retrieve a large key-value pair
hashMap.put(999999, 200);
System.out.println("Value for key 999999: " + hashMap.get(999999)); // Should print 200
// Insert multiple keys to simulate the constraints
for (int i = 0; i < 10000; i++) {
hashMap.put(i, i * 2);
}
System.out.println("Value for key 9999: " + hashMap.get(9999)); // Should print 19998
}
}