The get method

Next, we will implement the get method to retrieve a value given a key with the following code:

get(key) {  const position = this.hashCode(key);  const linkedList = this.table[position]; // {1}  if (linkedList != null && !linkedList.isEmpty()) { // {2}    let current = linkedList.getHead(); // {3}    while (current != null) { // {4}      if (current.element.key === key) { // {5}        return current.element.value; // {6}      }      current = current.next; // {7}    }  }  return undefined; // {8}}

The first verification we need to do is to check whether there is any value at the desired position. To do this, we retrieve the linkedList at the hash position ({1}) and we verify whether there is an instance of the linkedList or if it is empty ({2}). If there is ...

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.