Skip to main content

hash-table

hash-table System Class

Class Precedence List:

hash-table, t

Description:

Hash tables provide a way of mapping any object (a key) to an associated object (a value).

See Also:

Section 18.1 (Hash Table Concepts), Section 22.1.3.13 (Printing Other Objects)

Notes:

The intent is that this mapping be implemented by a hashing mechanism, such as that described in Section 6.4 “Hashing” of The Art of Computer Programming, Volume 3 (pp506-549). In spite of this intent, no conforming implementation is required to use any particular technique to implement the mapping.

Expanded Reference: hash-table

Overview

hash-table is a system class representing hash tables -- data structures that map keys to values using a hashing mechanism. Hash tables provide efficient O(1) average-case lookup, insertion, and deletion.

Basic Type Checking

;; Hash tables are objects of type hash-table
(let ((ht (make-hash-table)))
(type-of ht))
; Implementation-dependent, but will be a subtype of hash-table
; e.g. HASH-TABLE

(typep (make-hash-table) 'hash-table)
=> T

Class Precedence

;; hash-table's class precedence list is: hash-table, t
(subtypep 'hash-table 't)
=> T
=> T

Hash Tables Are Not Sequences or Lists

;; Hash tables are distinct from other mapping structures
(typep (make-hash-table) 'sequence)
=> NIL

(typep (make-hash-table) 'list)
=> NIL

;; Alists and plists are not hash tables
(hash-table-p '((a . 1) (b . 2)))
=> NIL

Practical Usage

;; Hash tables are created with make-hash-table and used via gethash
(let ((ht (make-hash-table :test #'equal)))
(setf (gethash "name" ht) "Alice")
(setf (gethash "age" ht) 30)
(values (gethash "name" ht)
(gethash "age" ht)))
=> "Alice"
=> 30