Problem
In this exercise, you need to implement a simple Java class called MyHashMap which will simulate the some basic behaviors of a HashMap in Java. You will implement the following methods:
-
put(int key, int value): This method will insert a (key, value) pair into the HashMap. If the key already exists in the HashMap, it should update the value associated with that key.
-
get(int key): This method will return the value associated with a key. If the key is not found in the HashMap, it should return -1.
-
remove(int key): This method will remove the key and its corresponding value from the HashMap.
Constraints
- All keys and values will be in the range of
[0, 1000000].
- The number of operations will be in the range of
[1, 10000].
- Do not use the built-in HashMap library.
Example
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 (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 (not found), The map is now [[1,1]]
Test Cases
| Operation | Argument | Expected Output |
|---|
| put | (1,1) | None |
| put | (2,2) | None |
| get | 1 | 1 |
| get | 3 | -1 |
| put | (2, | |