The put method

Let's implement the first method, the put method, as follows:

put(key, value) {  if (key != null && value != null) {    const position = this.hashCode(key);    if (this.table[position] == null) { // {1}      this.table[position] = new LinkedList(); // {2}    }    this.table[position].push(new ValuePair(key, value)); // {3}    return true;  }  return false;}

In this method, we will verify whether the position we are trying to add the value to already has other values in it ({1}). If this is the first time we are adding an element in this position, we will initialize it with an instance of the LinkedList class ({2} - which we learned in Chapter 6, Linked Lists). Then, we will add the ValuePair instance to the LinkedList instance using the push method ...

Get Learning JavaScript Data Structures and Algorithms - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.