store/[org.clojure/clojure "1.6.0"] clj::clojure.core/contains?

Community Documentation

Edit

It is a common mistake to think of the English meaning of the word 'contains', and believe that therefore contains? will tell you whether a vector or array contains a value. See 'some' if that is what you want.

contains? is good for checking whether a map has a mapping for a key, or a set contains an element. It can be easier to use correctly than 'get', especially if you wish to allow a key, value, or set element to be nil.

See also:

Source

(defn contains?
  "Returns true if key is present in the given collection, otherwise
  returns false.  Note that for numerically indexed collections like
  vectors and Java arrays, this tests if the numeric key is within the
  range of indexes. 'contains?' operates constant or logarithmic time;
  it will not perform a linear search for a value.  See also 'some'."
  {:added "1.0"
   :static true}
  [coll key] (. clojure.lang.RT (contains coll key)))

Example 1

Edit
;; It is not as useful, but contains? can also determine whether a number
;; lies within the range of defined indices of a vector, string, or Java
;; array.  For strings and Java arrays, it is identical in these cases to
;; (and (0 <= i) (< i (count coll))) where i is equal to (. key intValue).
;; The behavior is the same for vectors, except only integer values
;; can return true.

;; max index for this string is 5
(contains? "abcdef" 5)
;; => true

;; max index for this vector is 2

(contains? [:a :b :c] 1)
;; => true

;; max index for this array is 3

(contains? (int-array [28 35 42 49]) 10)
;; => false

Example 2

Edit
;; :b is in the set
(contains? #{:a :b 5 nil} :b)
;; => true

;; 2 is not

(contains? #{:a :b 5 nil} 2)
;; => false

;; nil is in this set

(contains? #{:a :b 5 nil} nil)
;; => true

;; but not in this one

(contains? #{:a :b 5} nil)
;; => false

Example 3

Edit
;; The intValue conversion can lead to unexpected behavior, demonstrated
;; below.  This happens because intValue converts the long and double
;; values shown to 0, which is in the range [0,2] of indices.

(def long-truncates-to-int-0 (bit-shift-left 1 33))

(contains? "abc" long-truncates-to-int-0)
;; => true

(contains? "abc" -0.99)
;; => true

(contains? [:a :b :c] long-truncates-to-int-0)
;; => true

;; only integer values can return true for vectors
(contains? [:a :b :c] 0.5)
;; => false       

Example 4

Edit
;; key :a is in the map
(contains? {:a "a" nil "nil"} :a)   
;; => true

;; :b is not

(contains? {:a "a" nil "nil"} :b)  
;; => false

;; nil is a key here

(contains? {:a "a" nil "nil"} nil)  
;; => true

;; but not here

(contains? {:a "a"} nil)
;; => false

Example 5

Edit
;; `contains?` is straightforward for maps:
(contains? {:a 1} :a)
;; => true

(contains? {:a nil} :a)
;; => true

(contains? {:a 1} :b)
;; => false

;; It's likely to surprise you for other sequences because it's 
;; about *indices*, not *contents*:
(contains? [:a :b :c] :b)
;; => false

(contains? [:a :b :c] 2)
;; => true

(contains? "f" 0)
;; => true

(contains? "f" 1)
;; => false

;; It can be applied to non-sequences:
(contains? 5 3)
;; => false

;; Although lists are sequences, `contains?` seems to always return
;; `false` for them. (Clojure 1.1)
;;
;; Note, this no longer works in Clojure 1.5, throwing an 
;; IllegalArgumentException.
(contains? '(1 2 3) 1)
;; => false

Example 6

Edit
;; Can be used to test set membership
(def s #{"a" "b" "c"})
;; => #'user/s

(contains? s "a")
;; => true

(contains? s "z")
;; => false

Uses on crossclj