What are the primary features of a Hash Map, and how does it handle collisions?
Instruction: Discuss the key characteristics of a Hash Map and explain the strategies used to resolve collisions.
Updated
Official answer
A hash map associates unique keys with values using a hash-based index plus key-equality checks. Inserting an equal key normally replaces its mapped value; inserting a distinct key with the same hash must preserve both entries.
For example, suppose keys A and B both hash to bucket 3. A chained table can store both in that bucket and compare keys during lookup. An open-addressed table probes alternative slots and follows the same probe rules when looking up B. Neither may treat a matching hash alone as proof that A=B.
Keys must obey the implementation's equality/hash contract while stored. Mutating a field used by hashing or equality can make an entry unreachable through normal lookup. Resizing, deletion markers, capacity and load thresholds also affect behavior.
Expected constant-time access relies on suitable hashing and controlled load; worst cases and expensive key comparisons can cost more. Chaining is not unlimited free capacity, and open addressing has metadata and unused-slot costs. Iteration order and thread-safety are library contracts, not universal hash-map properties. Validate the actual implementation before relying on either, and test deliberately colliding keys and updates as well as normal lookups.