Hashtables

Definitions

What is a hashtable

Hash - A hash is the result of some algorithm taking an incoming string and converting it into a value that could be used for either security or some other purpose. In the case of a hashtable, it is used to determine the index of the array. Buckets - A bucket is what is contained in each index of the array of the hashtable. Each index is a bucket. An index could potentially contain multiple key/value pairs if a collision occurs. Collisions - A collision is what happens when more than one key gets hashed to the same location of the hashtable.

Uses

Hashtables are a data structure that utilize key value pairs

Hashtable works according to the priciple of storng the key into the data structure/hashtables, and quickly retrieve the value.

This is done through hash, which is a way to encode the key

Example

[“Greenwood:98103”, “Downtown:98101”, “Alki Beach:98116”, “Bainbridge Island:98110”]

Structure

Hashing

How a Hash is Created?

67 + 97 + 116 = 280

280 * 599 = 69648

69648 % 1024 = 16

Key gets placed in index of 16.

Hashmap Example:

SUM HASHED: Pioneer Square = 1379 SUM HASHED: Alki Beach = 884 SUM HASHED: U District = 955

BUCKET SIZE=99 SUM INDEX: 1379 % 99 = 92 SUM INDEX: 884 % 99 = 92 SUM INDEX: 995 % 99 = 64

Internal Methods