liveBook · Manning

Map

As explained in Clojure: The Essential Reference

A map is an associative Clojure collection that stores values under keys.

A map is an associative collection that stores values under keys. Map operations distinguish key presence from value truthiness, making explicit membership checks important when a key maps to nil. section 8.2.3

Use these operations to inspect map structure and membership:

  • contains? checks whether a key exists in hash maps, sorted maps, array maps, and Java map types; it returns false when a key is absent, even if a value would be truthy elsewhere. section 8.2.3
  • keys returns a sequence of keys, while vals returns a sequence of values. Ordering depends on the map type but remains consistent for the same input. section 11.2.1
  • Both functions also accept Java maps and collections of java.util.Map$Entry objects; invalid map-like inputs can raise ClassCastException. section 11.2.1

Test presence when nil is a valid value

(def m {:a 1 :b nil :c 3}) (if (get m :b) "Key found" "Key not found") ;; "Key not found" (if (contains? m :b) "Key found" "Key not found") ;; "Key found"

How Clojure: The Essential Reference covers it

How deep it goes
Defines supported map-like inputs, return behavior, ordering, and notable exceptions without detailing map implementation internals.
Examples
Demonstrates nil-valued entries, key membership, extracted sequences, filtered MapEntry values, and configuration scoring.
What you can do after
Apply map membership checks with contains? and extract keys or values with keys and vals.
How it connects
Connects maps with associative collections, vectors, sets, Java Map implementations, and MapEntry sequences.

Also explained in

Compare how 42 books explain Map →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Clojure: The Essential Reference. The section links point to the parts of the book it draws on.