At this point, we have our building blocks, so let's go ahead and implement the set method. The method will:
- take the
keypassed - run it through the hash function, and
- set the value in our
storageat that particular index
Notice the way we're storing it as well: each index in this._storage (this._storage[idx]) is itself an array, thereby primitively solving for the collision problem.
1public void set(String key, Object val) {
2 int idx = hashStr(key);
3
4 if (this._storage[idx] == null) {
5 this._storage[idx] = new ArrayList<>();
6 }
7
8 this._storage[idx].add(new Object[] {key, val});
9}