You're viewing version 1.2.0 of array-map. The latest stable version of Clojure Core is 1.3.0.
1.2.0 Arrow_down_16x16



  • (array-map)
  • (array-map & keyvals)
Constructs an array-map.

3 Examples top

  • user=> (array-map [1 2] [3 4 5])
    {[1 2] [3 4 5]}
  • user=> (array-map :a 10)
    {:a 10}
    user=> (array-map :a 10 :b 20)
    {:a 10 :b 20}
    user=> (apply array-map [:a 10 :b 20 :c 30])
    {:a 10 :b 20 :c 30}
    user=> (apply assoc {} [:a 10 :b 20 :c 30]) ;same result using assoc
    {:a 10 :b 20 :c 30}
  • user=> (keys (assoc (array-map :foo 10 :bar 20) :baz 30))
    (:baz :foo :bar)
    ; baz is first; :foo and :bar follow the order given to array-map
Log in to add / edit an example.

See Also top

Log in to add a see also.

Plus_12x12 Minus_12x12 Source clojure/core.clj:3387 top

(defn array-map
  "Constructs an array-map."
  {:added "1.0"}
  ([] (. clojure.lang.PersistentArrayMap EMPTY))
  ([& keyvals] (clojure.lang.PersistentArrayMap/createWithCheck (to-array keyvals))))
Vars in clojure.core/array-map: defn to-array

Comments top

2 comment(s) for array-map.

The definition is kind of short, IMO. More descriptively, array-map creates a mapping with arrays being the keys and the values. It doesn't seem like array-map cares whether or not the keys/values are arrays, although it doesn't seem to like sequences.

An array-map maintains the insertion order of the keys. Look up is linear, which is not a problem for small maps (say less than 10 keys). If your map is large, you should use hash-map instead.

When you assoc onto an existing array-map, the result is a new array-map with the new key as the first key. The rest of the keys are in the same order as the original. Functions such as seq and keys will respect the key order.

Note that assoc will decide to return a hash-map if the result is too big to be efficient.

Log in to add a comment.