Clojure - Maps merge



Merges two maps entries into one single map entry.

Syntax

Following is the syntax.

(merge hmap1 hmap2)

Parameters − ‘hmap1’ is the map of hash keys and values. ‘hmap2’ is the map of hash keys and values, which needs to be mapped with the first HashMap.

Return Value − Returns a combined HashMap of both hasmap1 and hasmap2.

Example

Following is an example of merge in Clojure.

(ns clojure.examples.example
   (:gen-class))
(defn example []
   (def demokeys (hash-map "z" 1 "b" 2 "a" 3))
   (def demokeys1 (hash-map "a" 2 "h" 5 "i" 7))
   (println (merge-with + demokeys demokeys1)))
(example)

Output

The above code produces the following output.

{z 1, x 4, a 3, i 7, b 2, h 5}
clojure_maps.htm
Advertisements