Hash Table
Also known as: Hash map, Dictionary, Associative array
Data structure that stores key-value pairs and uses a hash function to compute where each item lives, giving very fast average lookups.
A hash table maps keys to values, letting a program look up a value by its key, such as a phone number by name or a product by its ID, in roughly constant time on average, written O(1) in Big O notation, however many entries are stored. It works by passing each key through a hash function, which turns it into a number used as an index into an array of slots called buckets. To find a key later, the program hashes it again and goes straight to the right bucket rather than searching the whole collection. Python's dict and Java's HashMap are hash tables.
Two different keys can hash to the same bucket, a collision, and every hash table must handle it. Separate chaining stores a small list of entries in each bucket, while open addressing probes for the next free slot according to a fixed rule. As the table fills, collisions grow more frequent, so implementations resize it, typically doubling the array and reinserting every entry, once a load factor threshold is passed. Hans Peter Luhn of IBM described hashing with chaining in a 1953 internal memorandum, one of the earliest known accounts of the technique.
Hash tables are among the most heavily used data structures in software, powering caches, database indexes, symbol tables in compilers, sets and the objects of dynamic languages. Their speed depends on a hash function that spreads keys evenly, and their worst case is poor. In 2011 researchers showed that many web platforms could be slowed to a crawl by requests crafted so that all keys collided, a denial of service that led several languages to adopt randomized hashing. These lookup hashes differ from cryptographic hash functions, which must meet stricter security requirements.
Key points
- A hash table uses a hash function to turn each key into an array index, giving fast average lookups.
- Collisions, where keys share a bucket, are handled by chaining or by probing for open slots.
- Tables resize as they fill to keep collisions rare and lookups fast.
- Crafted collisions can cause denial of service, which led several languages to randomize their hash functions.
Related entries
Data Structure
Way of organizing and storing data in a computer so that it can be accessed and modified efficiently, such as an array, list, tree or hash table.
Big O Notation
Mathematical notation describing how an algorithm's running time or memory use grows as its input size grows, ignoring constant factors.
Cryptographic Hash Function
Function that maps data of any size to a fixed-length digest in a way that is practically impossible to reverse or to match with different input.
Memoization
Optimization technique that caches a function's results for given inputs so that repeated calls with the same arguments return instantly.
Database
Organized collection of structured data stored electronically and managed by software that lets users store, query and update it reliably.
Tree (Data Structure)
Hierarchical data structure of nodes connected by edges, with a single root and no cycles, used for file systems, indexes and parsers.
Python
High-level, general-purpose programming language created by Guido van Rossum, known for readable syntax and widely used in data science and AI.
Denial-of-Service Attack
Attack that makes a service, website or network unavailable to legitimate users, typically by overwhelming it with traffic or requests.
Featured in guides
Referenced by
Other entries that discuss or point to Hash Table.