How to add (and remove) key-value pairs in an Elixir map? This does not work:
map = %{a: 1, b: 2, c: 3} map[:d] = 4
HashMap remove() Method in Java HashMap. remove() is an inbuilt method of HashMap class and is used to remove the mapping of any particular key from the map. It basically removes the values for any particular key in the Map. Parameters: The method takes one parameter key whose mapping is to be removed from the Map.
Assuming your set contains the strings you want to remove, you can use the keySet method and map. keySet(). removeAll(keySet); . keySet returns a Set view of the keys contained in this map.
Use Map.put(map, key, value)
:
map = Map.put(map, :d, 4) #=> %{a: 1, b: 2, c: 3, d: 4}
Use Map.delete(map, key)
:
map = Map.delete(map, :b) #=> %{a: 1, c: 3}
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