Let's say I have an atom that contains a map like this:
{:count 0 :map hash-map}
How can I use swap to merge another key-value pair onto :map
?
To merge Maps, use the spread operator (...) to unpack the values of two or more Maps into an array and pass them into the Map () constructor, e.g. new Map ( [...map1, ...map2]). The new Map will contain the key-value pairs from all provided Map objects.
Clojure - Maps. A Map is a collection that maps keys to values. Two different map types are provided - hashed and sorted. HashMaps require keys that correctly support hashCode and equals. SortedMaps require keys that implement Comparable, or an instance of Comparator.
The merge (Key, Value, BiFunctional) method of HashMap class is used to combine multiple mapped values for a key using the given mapping function. Bucket is actually an index of array, that array is called table in HashMap implementation. So table [0] is referred to as bucket0, table [1] as bucket1 and so on.
You'd use assoc-in
:
(swap! my-atom assoc-in [:map :new-key] value)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With