Start by creating a HashMap
class or factory function. It’s up to you which you want to use. Then proceed to create the following methods:
- 1.
hash(key)
takes a key and produces a hash code with it. - 2.
set(key, value)
takes two arguments, the first is a key and the second is a value that is assigned to this key. If a key already exists, then the old value is overwritten or we can say that we update the key’s value (e.g.Carlos
is our key but it is called twice: once with valueI am the old value.
, and once with valueI am the new value.
. From the logic stated above,Carlos
should contain only the latter value). - 3.
get(key)
takes one argument as a key and returns the value that is assigned to this key. If a key is not found, returnnull
. - 4.
has(key)
takes a key as an argument and returnstrue
orfalse
based on whether or not the key is in the hash map. - 5.
remove(key)
takes a key as an argument. If the given key is in the hash map, it should remove the entry with that key and returntrue
. If the key isn’t in the hash map, it should returnfalse
. - 6.
length()
returns the number of stored keys in the hash map. - 7.
clear()
removes all entries in the hash map. - 8.
keys()
returns an array containing all the keys inside the hash map. - 9.
values()
returns an array containing all the values. - 10.
entries()
returns an array that contains eachkey, value
pair. Example:[[firstKey, firstValue], [secondKey, secondValue]]
- Create a
HashSet
class or factory function that behaves the same as aHashMap
but only containskeys
with novalues
.