Hash Table





Definition: A methodically stored collection of corresponding keys and values. Every key has a meaningful value that it is linked to (and typically meant to retrieve) and both are stored at a calculated location called a hash index.


Other common names for hash tables (some features may differ slightly from hash table):



Real Life Example: Think of hash tables as warehouse stores like Costco or Sam's Club. Every section in the store (outdoors, bakery, frosen foods, etc.) can be thought of as a hash index. The name of every item in the store can be thought of as a bunch of keys and the physical content of each item is the equivalent of a value. The placement of each item depends on its name, which relates it to a specified hash index (section in the store). In that section, the key (name of the item) and value (the physical item) are placed and stored.


Basic Properties:



Basic Hash Table Operations:

  1. Adding a key/value pair. This is referred to as "putting" a pair into the hashtable.
  2. Removing a key/value pair from the hashtable. This is done by removing the key (the value is linked to the key so it is automatically deleted as well).
  3. Changing the value of a key already in the hashtable.

Fun Fact: Despite hash tables being resizable, it is possible for a hash function to generate the same hash index for two different key/value pairs. This is a dilemma called hash collision. There are two common solutions to hash collision:

  1. Separate Chaining: The key/value pair is stored at the initially calculated hash index but as a node in a Linked List containing the key/value pair(s) that were already there.

  2. Open Addressing: The key/value pair is inserted at the closest empty hash index if it exists.


Commonly Used In:



Other Similar Data Structures: