Clojure - Map 是否包含?



檢視Map是否包含所需的鍵。

語法

以下是語法。

(contains hmap key)

引數 − ‘hmap’ 是雜湊鍵值對的Map。‘key’ 是需要在Map中搜索的鍵。

返回值 − 如果鍵存在,則返回 true;否則返回 false。

示例

以下是 Clojure 中 contains? 的示例。

(ns clojure.examples.example
   (:gen-class))
(defn example []
   (def demokeys (hash-map "z" "1" "b" "2" "a" "3"))
   (println (contains? demokeys "b"))
   (println (contains? demokeys "x")))
(example)

輸出

以上程式碼產生以下輸出。

true
false
clojure_maps.htm
廣告
© . All rights reserved.