Browse Clojure Foundations for Java Developers

Maps

Clojure’s go-to “record” type: persistent maps with keyword keys, fast lookup, and immutable assoc/update/merge operations.

Maps are the default “domain object” in most Clojure codebases. If you’re coming from Java, think: a map often replaces a POJO + getters/setters—except updates are immutable and explicit.

Create

1{:user/id 42
2 :user/name "Ada"
3 :user/active? true}

Keyword keys are a common convention because they are lightweight, immutable, and readable.

Read

1(def user {:user/id 42 :user/name "Ada"})
2
3(get user :user/name)   ; => "Ada"
4(:user/name user)       ; => "Ada"  ; keyword-as-function lookup
5(user :user/id)         ; => 42     ; maps are also callable by key

Update

1(assoc user :user/active? true)
2(update user :user/id inc)
3(dissoc user :user/name)

Merge maps (rightmost wins on key collisions):

1(merge {:a 1} {:a 2 :b 3})
2;; => {:a 2, :b 3}

Nested Access/Updates

1(get-in {:a {:b 1}} [:a :b])          ; => 1
2(assoc-in {} [:a :b] 1)               ; => {:a {:b 1}}
3(update-in {:a {:b 1}} [:a :b] + 10)  ; => {:a {:b 11}}

Knowledge Check: Maps

### What does `(:a {:a 1 :b 2})` evaluate to? - [x] `1` - [ ] `:a` - [ ] `{:a 1}` - [ ] It throws because keywords aren’t callable. > **Explanation:** Keywords are callable and do map lookup: `(:a m)` is shorthand for `(get m :a)`. ### What does `(update {:a 1} :a inc)` evaluate to? - [x] `{:a 2}` - [ ] `{:a 1}` (because maps are immutable) - [ ] `2` - [ ] It mutates the map and returns `nil`. > **Explanation:** `update` returns a new map where the key’s value is replaced with the result of applying a function. ### What does `(merge {:a 1} {:a 2 :b 3})` evaluate to? - [x] `{:a 2 :b 3}` - [ ] `{:a 1 :b 3}` - [ ] `{:a 1}` - [ ] `{:a 1 :a 2 :b 3}` > **Explanation:** On key collisions, `merge` keeps the rightmost value.
Revised on Friday, April 24, 2026