Browse Clojure Foundations for Java Developers

Vectors

Clojure’s default ordered collection: persistent vectors with fast indexed access, assoc updates, and conj at the end.

Vectors are Clojure’s default choice for ordered data. They’re persistent (immutable with structural sharing) and provide fast indexed access—similar to an immutable ArrayList.

Create

1[1 2 3]
2(vector 1 2 3)

Read (Indexed)

1(def v [10 20 30])
2
3(get v 1)   ; => 20
4(nth v 1)   ; => 20
5(v 1)       ; => 20  ; vector-as-function lookup

Update and Grow

1(assoc [10 20 30] 1 99) ; => [10 99 30]
2(conj  [1 2] 3)         ; => [1 2 3]   ; vectors conj at the end

Stack-Like Ops (peek / pop)

1(peek [1 2 3]) ; => 3
2(pop  [1 2 3]) ; => [1 2]

Slice with subvec

1(subvec [0 1 2 3] 1 3) ; => [1 2]

subvec uses start/end indexes and the end is exclusive (like Java’s substring and many slice APIs).

Knowledge Check: Vectors

### What does `(peek [1 2 3])` return? - [x] `3` - [ ] `1` - [ ] `[1 2]` - [ ] `nil` > **Explanation:** `peek` returns the “top” element for the collection. For vectors, that’s the last element. ### What does `(conj [1 2] 3)` evaluate to? - [x] `[1 2 3]` - [ ] `(3 1 2)` - [ ] `[3 1 2]` - [ ] It mutates the vector and returns `nil`. > **Explanation:** `conj` adds at the most efficient place. For vectors, that’s the end. ### What does `(subvec [0 1 2 3] 1 3)` evaluate to? - [x] `[1 2]` - [ ] `[1 2 3]` - [ ] `[0 1 2]` - [ ] `(1 2)` > **Explanation:** `subvec` takes a start index (inclusive) and end index (exclusive) and returns a vector slice.
Revised on Friday, April 24, 2026