Search code examples
clojureclojure-java-interop

How to merge two hashmaps on Clojure?


How can I merge two hashmap variables, for example map1 and map2?

I have tried (merge map1 map2), but I got the following exception:

ClassCastException java.util.HashMap cannot be cast to clojure.lang.IPersistentCollection


Solution

  • well, obviously because java.util.HashMap is not clojure map. You probably get map1 or map2 (or both) executing some java code?

    so in this case you should first convert them to clojure maps like this, for example:

    (merge (into {} map1) (into {} map2))

    this should work.

    also read this one: Clojure: working with a java.util.HashMap in an idomatic Clojure fashion