1)建立哈希表java
用make-hash-table函数建立哈希表:python
CL-USER> (defparameter *my-hash* (make-hash-table)) *MY-HASH* CL-USER> (setf (gethash 'one-entry *my-hash*) "one") ;;赋值 "one" CL-USER> (setf (gethash 'another-entry *my-hash*) 2/4) 1/2 CL-USER> (gethash 'one-entry *my-hash*) "one" T CL-USER> (gethash 'another-entry *my-hash*) 1/2
2)检测所建立的哈希表函数
CL-USER> (if (gethash 'one-entry *my-hash*) "Key exists" "Key does not exist") "Key exists" CL-USER> (if (gethash 'another-entry *my-hash*) "Key exists" "Key does not exist") "Key exists" CL-USER> (setf (gethash 'another-entry *my-hash*) nil) NIL CL-USER> (if (gethash 'another-entry *my-hash*) "Key exists" "Key does not exist") "Key does not exist" CL-USER> (if (nth-value 1 (gethash 'another-entry *my-hash*)) "Key eixst" "Key does not exist") "Key eixst" CL-USER> (if (nth-value 1 (gethash 'no-entry *my-hash*)) "Key eixts" "Key does not exits") "Key does not exits"
3)删除哈希表oop
CL-USER> (defparameter *my-hash* (make-hash-table)) *MY-HASH* CL-USER> (setf (gethash 'first-key *my-hash*) 'one) ONE CL-USER> (gethash 'first-key *my-hash*) ONE T CL-USER> (remhash 'first-key *my-hash*) T CL-USER> (gethash 'first-key *my-hash*) NIL NIL
4)遍历哈希表spa
a、能够用maphash函数遍历哈希表:
code
CL-USER> (defparameter *my-hash* (make-hash-table)) *MY-HASH* CL-USER> (setf (gethash 'first-key *my-hash*) 'one) ONE CL-USER> (setf (gethash 'second-key *my-hash*) 'two) TWO CL-USER> (setf (gethash 'third-key *my-hash*) nil) NIL CL-USER> (setf (gethash nil *my-hash*) 'nil-value) NIL-VALUE CL-USER> (defun print-hash-entry (key value) (format t "The value associated with the key ~S is ~S~%" key value)) PRINT-HASH-ENTRY CL-USER> (maphash #'print-hash-entry *my-hash*) The value associated with the key FIRST-KEY is ONE The value associated with the key SECOND-KEY is TWO The value associated with the key THIRD-KEY is NIL The value associated with the key NIL is NIL-VALUE NIL CL-USER> (defun print-hash-entry (key value) (format t "~S ~S~%" key value)) CL-USER> (maphash #'print-hash-entry *my-hash*) FIRST-KEY ONE SECOND-KEY TWO THIRD-KEY NIL NIL NIL-VALUE NIL
b、也能够用with-hash-table-iterator函数进行遍历,相对复杂一点:orm
CL-USER> (defparameter *my-hash* (make-hash-table)) *MY-HASH* CL-USER> (setf (gethash 'first-key *my-hash*) 'one) ONE CL-USER> (setf (gethash 'second-key *my-hash*) 'two) TWO CL-USER> (setf (gethash 'third-key *my-hash*) nil) NIL CL-USER> (setf (gethash 'nil *my-hash*) 'nothing) NOTHING CL-USER> (with-hash-table-iterator (my-iterator *my-hash*) (loop (multiple-value-bind (entry-p key value) (my-iterator) (if entry-p (print-hash-entry key value) (return))))) FIRST-KEY ONE SECOND-KEY TWO THIRD-KEY NIL NIL NOTHING NIL
c、一样用loop循环遍历ip
CL-USER> (loop for key being the hash-keys of *my-hash* do (print key)) FIRST-KEY SECOND-KEY THIRD-KEY NIL NIL
d、loop循环遍历:ci
CL-USER> (loop for key being the hash-keys of *my-hash* using (hash-value value) do (format t "The value associated with the key ~S is ~S~%" key value)) The value associated with the key FIRST-KEY is ONE The value associated with the key SECOND-KEY is TWO The value associated with the key THIRD-KEY is NIL The value associated with the key NIL is NOTHING NIL CL-USER> (loop for value being the hash-values of *my-hash* do (print value)) ONE TWO NIL NOTHING NIL CL-USER> (loop for value being the hash-values of *my-hash* using (hash-key key) do (format t "~&~A -> ~A" key value)) FIRST-KEY -> ONE SECOND-KEY -> TWO THIRD-KEY -> NIL NIL -> NOTHING NIL
5)计算哈希表数据数量rem
用hash-table-count函数:
CL-USER> (defparameter *my-hash2* (make-hash-table)) *MY-HASH2* CL-USER> (hash-table-count *my-hash2*) ;;count the entries in a hash table 0 CL-USER> (setf (gethash 'first *my-hash2*) 'one) ONE CL-USER> (setf (gethash 'second *my-hash2*) 2) 2 CL-USER> (setf (gethash 'third *my-hash2*) 3) 3 CL-USER> (hash-table-count *my-hash2*) 3 CL-USER> (setf (gethash 'second *my-hash2*) 'two) TWO CL-USER> (hash-table-count *my-hash2*) 3 CL-USER> (clrhash *my-hash2*) ;;clean hash table #<HASH-TABLE :TEST EQL :COUNT 0 {25045971}> CL-USER> (hash-table-count *my-hash2*) 0