Browse Clojure Foundations for Java Developers

Collection Literals

Quick reference for Clojure collection literals and the operations you’ll use most on lists, vectors, maps, and sets.

Clojure programs and Clojure data use the same persistent collections. Once you recognize the literal shapes, you can “read the data” and understand most code by inspection.

Literal Cheat Sheet

1(a b c)      ; list (usually code)
2'(a b c)     ; list as data (quoted)
3[a b c]      ; vector (ordered data)
4{:a 1 :b 2}  ; map (associative data)
5#{:a :b}     ; set (membership / uniqueness)

Lists vs Vectors (the one gotcha)

Lists are optimized for adding/removing at the front. Vectors are optimized for indexed access.

1(conj [1 2] 3)   ; => [1 2 3]
2(conj '(1 2) 3)  ; => (3 1 2)

If you’re coming from Java, you’ll typically use vectors and maps for most domain data. You’ll see lists most often in code and in macro examples.

Maps: the default “object” replacement

Represent a user/order/config as a map, typically with keyword keys:

1(def user {:user/id 42
2           :user/name "Ada"})
3
4(:user/id user)               ; => 42
5(get user :user/name)         ; => "Ada"
6(assoc user :user/active? true)
7(update user :user/id inc)

Nested access and updates stay readable with get-in, assoc-in, and update-in:

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}}

Sets: uniqueness and membership

1(def roles #{:admin :billing})
2
3(contains? roles :admin) ; => true
4(roles :admin)           ; => :admin   ; set-as-function membership
5(roles :guest)           ; => nil

Knowledge Check: Collections

### What do these evaluate to? - [x] `(conj [1 2] 3)` returns `[1 2 3]`, and `(conj '(1 2) 3)` returns `(3 1 2)`. - [ ] `(conj [1 2] 3)` returns `(3 1 2)`, and `(conj '(1 2) 3)` returns `[1 2 3]`. - [ ] Both return `(1 2 3)`. - [ ] Both mutate the existing collection and return `nil`. > **Explanation:** `conj` adds in the most efficient place for the collection: end for vectors, front for lists. ### Which is the most idiomatic way to read a value from a map `m` with keyword key `:k`? - [x] `(:k m)` or `(get m :k)` - [ ] `m.k` (like Java field access) - [ ] `(m :k)` only works for vectors - [ ] `(k m)` where `k` is a string > **Explanation:** Keywords are callable, so `(:k m)` is a common map-lookup idiom. `get` is equally fine. ### What does `update` do? - [x] It applies a function to an existing key’s value and returns an updated map. - [ ] It mutates the map in place. - [ ] It updates every key in the map. - [ ] It only works on Java objects via interop. > **Explanation:** Persistent collections are immutable; `update` returns a new map with the key changed.
Revised on Friday, April 24, 2026