Collections
Vectors, maps, sets, lists, and sequences—Clojure’s core data structures.
Clojure’s core data structures are immutable persistent collections. That phrase is worth learning early:
- Immutable: operations don’t modify a collection in place.
- Persistent: “updates” return a new collection while sharing structure with the old one (so it’s usually fast enough for real systems).
Java mental model: instead of “mutate an ArrayList”, you usually “produce a new value” and pass it forward—making tests, concurrency, and reasoning simpler.
The big four (+ one abstraction)
- Vector
[]: your default “ordered collection” (think: immutable, value-oriented cousin of ArrayList).
- Map
{}: key/value data (think: immutable HashMap that you build up via assoc, update, dissoc).
- Set
#{}: membership and uniqueness.
- List
(): shows up a lot as code (forms) and in some sequence processing; less common as an everyday data container.
- Seq: an abstraction over “things you can iterate”. Most of Clojure’s collection functions (
map, filter, reduce, …) speak in seqs.
A few practical gotchas (worth memorizing)
conj adds to the end of a vector, but the front of a list.
- Many functions return lazy sequences, which is great for composition but changes when work happens.
The goal of this section is to make you fluent at reading collection literals, predicting what common operations return, and choosing the default collection that keeps your code simple.
In this section
-
Clojure Lists: Understanding and Using Lists in Functional Programming
Explore the fundamentals of lists in Clojure, a key data structure in functional programming, and learn how to create, access, and utilize them effectively.
-
Clojure Vectors: Efficient Indexed Collections for Java Developers
Explore Clojure vectors as efficient, indexed, random-access collections. Learn how to create, access, and manipulate vectors, and understand their advantages over Java arrays and lists.
-
Clojure Maps: Key-Value Pairs, Access, and Manipulation
Explore Clojure maps as key-value pairs, including creation, access, and manipulation techniques for Java developers transitioning to Clojure.
-
Clojure Sets: Unique Collections and Operations
Explore Clojure sets, their creation, membership checking, and operations like union and intersection, tailored for Java developers transitioning to Clojure.