Lisp - Iterating a Hash Table



In LISP, we've Hash Table as a a very useful data structure to store key-value pairs. LISP provides maphash function and a low level function with-hash-table-iterator to iterate over hash table entries.

In this chapter, we'll be checking how to add and print all values of a hashtable with examples.

Creating and adding values to hash table for numbers/symbols

We can create a hashtable for symbols and numbers with default equality function: eql.

(defvar my-hash-table (make-hash-table))
(setf (gethash '001 my-hash-table) 10)
  • make-hash-table− make-hash-table without any arguments creates a new hash table my-hash-table.

  • gethash− retrieves the value associated with key 001 in my-hash-table. If key is not present in table, then nil is returned.

  • setf− sets the value 10 associated with key 001

Iterating a hashtable using maphash function

We can iterate all elements of a hashtable using maphash function as shown below:

(maphash function hashtable-name)
  • maphash− iterate over entries of a hashtable

  • hashtable-name− name of the hashtable.

Example - Adding and Printings Entries of Hashtable of Numbers

main.lisp

; create a hashtable
(defvar my-hash-table (make-hash-table)) 

; add key-value pairs to hash table
(setf (gethash '001 my-hash-table) 10)
(setf (gethash '002 my-hash-table) 20) 
(setf (gethash '003 my-hash-table) 30) 

; print all entries
(maphash #'(lambda (key value)
   (format t "Key: ~a, Value: ~a~%" key value))
   my-hash-table)

Output

When you execute the code, it returns the following result −

Key: 3, Value: 30
Key: 2, Value: 20
Key: 1, Value: 10

In case of String, we should use equal function other than default function eql

Example - Adding and Iterating Strings of Hashtable

main.lisp

; create a hashtable
(defvar my-hash-table (make-hash-table :test #'equal)) 

; add key-value pairs to hash table
(setf (gethash "apple" my-hash-table) 10)
(setf (gethash "banana" my-hash-table) 20) 
(setf (gethash "orange" my-hash-table) 30) 

; print all entries
(maphash #'(lambda (key value)
   (format t "Key: ~a, Value: ~a~%" key value))
   my-hash-table)

Output

When you execute the code, it returns the following result −

Key: orange, Value: 30
Key: banana, Value: 20
Key: apple, Value: 10

Iterating a hashtable using with-hash-table-iterator function

We can iterate all elements of a hashtable using with-hash-table-iterator function as shown below:

(with-hash-table-iterator (iterator my-hash-table)
  (loop
     (multiple-value-bind (present? key value) (iterator)
         (if present?
            (format t "Key: ~a, Value: ~a~%" key value)
               (return)))))
  • with-hash-table-iterator− iterate over entries of a hashtable using an iterator

  • hashtable-name− name of the hashtable.

Example - Adding and Iterating Hashtable using with-hash-table-iterator

main.lisp

; create a hashtable
(defvar my-hash-table (make-hash-table :test #'equal)) 

; add key-value pairs to hash table
(setf (gethash "apple" my-hash-table) 10)
(setf (gethash "banana" my-hash-table) 20) 
(setf (gethash "orange" my-hash-table) 30) 

(with-hash-table-iterator (iterator my-hash-table)
  (loop
     (multiple-value-bind (present? key value) (iterator)
         (if present?
            (format t "Key: ~a, Value: ~a~%" key value)
               (return)))))

Output

When you execute the code, it returns the following result −

Key: orange, Value: 30
Key: banana, Value: 20
Key: apple, Value: 10
Advertisements