ClojureDocs

Nav

Namespaces

get

clojure.core

Available since 1.0 (source)
  • (get map key)
  • (get map key not-found)
Returns the value mapped to key, not-found or nil if key not present
in associative collection, set, string, array, or ILookup instance.
10 Examples
(get [1 2 3] 1)
;;=> 2

(get [1 2 3] 5)
;;=> nil

(get [1 2 3] 5 100)
;;=> 100

(get {:a 1 :b 2} :b)
;;=> 2

(get {:a 1 :b 2} :z "missing")
;;=> "missing"

;; to get an index of the element of a vector, use .indexOf
(def v ["one" "two" "three" "two"])
;; #'user/v

(.indexOf v "two")
;;=> 1

(.indexOf v "foo")
;;=> -1
;; the system environment has a hash-map semantic
(get (System/getenv) "SHELL")
;;=> "/bin/bash"

(get (System/getenv) "PATH")
;;=> "/usr/local/bin:/sbin:/usr/sbin:/usr/bin:/bin"
;; 'get' is not the only option
(def my-map {:a 1 :b 2 :c 3})

;; maps act like functions taking keys 
(my-map :a)
;;=> 1

;; even keys (if they are keywords) act like functions
(:b my-map)
;;=> 2
;; it is tempting to try an index on a list
(get '(a b c) 1)
;;=> nil

;; but you should use nth
(nth '(a b c) 1)
;;=> b
;; Get also works with strings:
(get "abc" 1)
;;=> \b
;; For sorted stuff, "key" must be of the same type of the existing keys. 
;; This allows descending the sorted tree in log(N) average.

(get (hash-map :a 1 :b 2) "a" "not found")
;; "not found"

(get (sorted-map :a 1 :b 2) "a" "not found")
;; ClassCastException

;; get works on transient maps, but silently fails on transient sets.
;; A similar issue affects contains?.

(get (transient #{0 1 2}) 1)
;; nil

;; get uses int cast with precision loss, with (.intValue x).
;; The example below is explained because 4294967296 equal 2^32, thus
;; (.intValue 4294967296) returns 0.
;; Be careful with sufficiently large keys:

(get ["a" "b" "c"] 4294967296)
;; "a"
;; Specifying 'not-found' does NOT guarantee that the return value is
;; not 'nil'.

(get {} :a 42)
;;=> 42

(get {:a nil} :a 42)
;;=> nil

;; This may be especially relevant if you use records, as all pre-defined keys
;; will usually be present.

(defrecord R [a b])
(def r (map->R {}))

r
;;=> #user.R{:a nil :b nil}

(get r :a 42)
;;=> nil

;; Consider using 'or' instead, as 'nil' is "falsy".

(or (get {:a nil} :a) 42)
;;=> 42
;; Specifying not-found doesn't mean that the expression won't be evaluated.
;; This can be a problem if you intend to materialize the not-found value 
;; only if key is not set.

(get {:a 1} :a (#(println "else")))
;; else
;;=> 1

;; using an `or` can solve your problem here
(or (get {:a 1} :a)
    (println "not found"))
;;=> 1
;; how to deal with nil map, defensive coding example:
;; try to get :a key from nil map 
(get nil :a :a1)
;;=> :a1
See Also

Returns a lazy sequence consisting of the result of applying f to 0 and the first item of coll, fo...

Added by mmwaikar

Returns the value in a nested associative structure, where ks is a sequence of keys. Returns nil i...

Added by Dimagog

Returns the map entry for key, or nil if key not present.

Added by AtKaaZ

Returns a map containing only those entries in map whose key is in keys

Added by alilee

Returns the value at the index. get returns nil if index out of bounds, nth throws an exception un...

Added by alilee
3 Notes