hash-map

added
1.0

ns
clojure.core

type
function

(hash-map) (hash-map & keyvals)

keyval => key val
Returns a new hash map with supplied mappings.  If any keys are
equal, they are handled as if by repeated uses of assoc.

                ;; create hash map the long way
user=> (hash-map)
{}

;; create hash map the short way
user=> {}
{}

;; sending a key more times, will remap it to the last value
user=> (hash-map :key1 1, :key1 2) 
{:key1 2} 

user=> {:key1 1, :key1 2}
IllegalArgumentException Duplicate key: :key1  clojure.lang.PersistentArrayMap.createWithCheck (PersistentArrayMap.java:70)


user=> (hash-map :key1 'val1, 'key2 :val2, [:compound :key] nil)
{[:compound :key] nil, :key1 val1, key2 :val2} 


            
                user=> (map #(hash-map % 0) (seq "abcdefgh"))
({\\a 0} {\\b 0} {\\c 0} {\\d 0} {\\e 0} {\\f 0} {\\g 0} {\\h 0}) 

user=> (apply hash-map (.split "a 1 b 2 c 3" " "))
{"a" "1", "b" "2", "c" "3"}
            
                ; a hash map can be stored in a var by using `def`
user=> (def person {:name "Steve" :age 24 :salary 7886 :company "Acme"})
#'user/person
user=> person
{:age 24, :name "Steve", :salary 7886, :company "Acme"}
            
                ;; Take a sequence of sequences (vector of vectors), and create a map
;; using date as the map key.
(def csv1 [["01/01/2012" 1 2 3 4]["06/15/2012" 38 24 101]])

(map #(hash-map (keyword (first %1)) (vec (rest %1))) csv1)
;;=> ({:01/01/2012 [1 2 3 4]} {:06/15/2012 [38 24 101]})

;; merge the list of maps into a single map
(apply merge '({"01/01/2012" [1 2 3 4]} {"06/15/2012" [38 24 101]}))
;;=> {"06/15/2012" [38 24 101], "01/01/2012" [1 2 3 4]}


            
                (apply hash-map [:a 1 :b 2])
;;=> {:b 2 :a 1}

;;is the same as
(def build-map (partial assoc {}))
(apply build-map [:a 1 :b 2])
;;=> {:b 2 :a 1}