Hash Tables: Complete
Last time, we got started with hash tables. We got up to setting a key-value pair. This time, we’re going to finish that up.
Here is the link: https://dev.to/miku86/javascript-data-structures-hash-table-intro-3nce
// Source: https://dev.to/miku86/javascript-data-structures-hash-table-intro-3nce// In terminal, run with// $ node --experimental-modules HashTable.js// This allows you to import modules in ES6 format/syntax// Looks for the closest parent `package.json` file// Make sure the closest package.json file contains the key-value pair:/*** {* "type": "module"* }*/class HashTable {constructor() {this.data = [];this.size = 0;}// Create a hash table by adding up all the UTF-16 values of each letter in the stringhash = (key) => {const chars = key.split('');const charCodes = chars.map(char => char.charCodeAt());const charCodeSum = charCodes.reduce((acc, cur) => acc + cur);return charCodeSum;}// Add a key-value pairset(key, value) {// Hash the keyconst hash = this.hash(key);if (!this.data[hash]) {this.data[hash] = [];}this.data[hash].push([key, value]);this.size++;}// Get data given a keyget = key => {const hash = this.hash(key);if (this.data[hash]) {// for (const element of this.data[hash]) {// if (element[0] === key) {// return element;// }// }let elementFound = null;this.data[hash].forEach(element => {if (element[0] === key) {elementFound = element;}})return elementFound;} elsereturn null;}keys = () => {const keys = [];this.data.forEach(bucket => {if (bucket) {bucket.forEach(item => {keys.push(item[0]);})}})// for (const bucket of this.data) {// if (bucket) {// for (const item of bucket) {// keys.push(item[0]);// }// }// }return keys;}values = () => {const values = [];this.data.forEach(bucket => {if (bucket) {bucket.forEach(item => {values.push(item[1]);})}})return values;}entries = () => {const entries = [];this.data.forEach(bucket => {if (bucket) {bucket.forEach(item => {entries.push(item);})}})return entries;}}// console.log("hash('name): ", hash('name'));// 417const newHashTable = new HashTable();console.log("newHashTable: ", newHashTable);// console.log("newHashTable.hash('name'): ", newHashTable.hash('name'));console.log("\nTesting set(key, value)");newHashTable.set("name", "miku86");console.log(newHashTable.data);newHashTable.set("mean", false);console.log(newHashTable.data);newHashTable.set("age", 33);console.log(newHashTable.data);console.log("\nTesting get(key)");console.log(newHashTable.get("name"));// [ 'name', 'miku86' ]console.log(newHashTable.get("mean"));// [ 'mean', false ]console.log(newHashTable.get("age"));// [ 'age', 33 ]console.log(newHashTable.get("nothing to see"));// nullconsole.log(newHashTable.get("naem"));// nullconsole.log("\nTesting keys()");console.log(newHashTable.keys());// [ 'age', 'name', 'mean' ]console.log("\nTesting values()");console.log(newHashTable.values());// [ 33, 'miku86', false ]console.log("\nTesting entries()");console.log(newHashTable.entries());// [['age', 33], ['name', 'miku86'], ['mean', false]]
We have the functions get(key)
, keys()
to get all the keys, values()
to get all the values, and entries()
to get all the key-value pairs/entries.
get(key)
gets a value, given a key. keys()
gets all the keys. values()
gets all the values. And entries()
gets all the entries.
I also have some console.logs
on the bottom for testing.
From what I’ve read as I coded along to the tutorial, this code is good to learn about hash tables under the hood, but it’s always better to use the hash table created by whatever language you’re using. It’s a lot more efficient than creating your own hash table. But at least now, you sort of understand what’s happening under the hood.
Wash your hands, stay safe and stay 6 feet apart!