Search code examples
clojure

Get key-value from Mutable Map in Clojure


I have created mutable state map using an atom. I would like to get the value of the key from the map. I have tried it in the below way, But it returns "nil" value

(def coll1 (atom {}) )
(swap! coll1 assoc :a "XXXX" :b "XXXXXX")

This statement printing

(println coll1)

#object[clojure.lang.Atom 0x771a660 {:status :ready, :val {:a XXXX, :b XXXXXX}}]

I have written this statement to fetch value of :a

(println (get-in coll1 [:val :a]))

Solution

  • You need to dereference the atom. This link will help. For your case, you are looking for something like (get @coll1 :a) or (:a @coll1) or (@coll1 :a)