An associative container supports access by key or numeric index. In Clojure, maps use keys and vectors use indexes; records are associative too. Sets do not implement the Associative interface, although some lookup functions support them. section 8.2.3 section 11.3.2
Use contains? to ask whether a lookup position exists—not whether a collection contains a particular value. It tests keys in maps, members in sets, and valid indexes in vectors. Thus (contains? [:a :b :c] 1) is true, while (contains? [:a :b :c] :a) is false. It does not work on every collection: queues and Java ArrayLists, for example, raise an exception. section 8.2.3
The same keyed structure supports several practical operations:
- select-keys extracts specified entries into a map: (select-keys {:a 1 :b 2} [:a]) returns {:a 1}. Given a vector, its result is still a map keyed by indexes. section 11.2.3
- get-in follows a sequence of keys through nested data: (get-in {:a {:b 3}} [:a :b]) returns 3. Supply a not-found value when a missing path must be distinguishable from nil. section 11.2.3
- update and update-in return a new structure after applying a function to an existing value. For example, (update-in {:a {:b 2}} [:a :b] inc) returns {:a {:b 3}}. section 11.3.2
Check presence, not truthiness
For {:a 1 :b nil}, (get m :b) yields nil even though :b exists. (contains? m :b) returns true. Use the presence test when nil is a legitimate stored value. section 8.2.3
A missing map key passed to update is created using the result of calling its function with nil; (fnil inc 0) makes this useful for counters. Vector indexes have different limits: updating at the end can append, but an index farther beyond it raises an exception. The key must be an integer. section 11.3.2

