Elisp: Test Hash Tables Equality 📜

By Xah Lee. Date: . Last updated: .
(defun xah-hashtable-equal (ztable-a ztable-b)
  "Return t if two hashtable are same, else nil.
Same means
• count of keys is same
• keys are same by `equal'
• values are same by `equal'

URL `http://xahlee.info/emacs/emacs/elisp_hash_table_equality.html'
Created: 2024-04-20
Version: 2026-02-17"
  (interactive)
  (let ((xnot-found ':SHmBFfxjpm))
    (catch 1111
      (when (not (eq (hash-table-count ztable-a) (hash-table-count ztable-b))) (throw 1111 nil))
      (maphash
       (lambda (kk vv)
         (when (eq xnot-found (gethash kk ztable-b xnot-found)) (throw 1111 nil))
         (when (not (equal vv (gethash kk ztable-b))) (throw 1111 nil)))
       ztable-a)
      t)))

Test

(setq x-a
      #s(hash-table
         size 30
         test equal
         data (
               "aa" 3
               "bb" 9
               "cc" 5 )))

(setq x-b
      #s(hash-table
         size 30
         test equal
         data (
               "bb" 9
               "aa" 3
               "cc" 5 )))

(xah-hashtable-equal x-a x-b )

Elisp, Hash Table