Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add / Remove key-value pairs from a Map

Tags:

elixir

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 
like image 227
Sheharyar Avatar asked Oct 25 '16 23:10

Sheharyar


People also ask

How do you remove a key value pair from a map?

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.

How do I get rid of multiple keys on a 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.


1 Answers

Add to Map

Use Map.put(map, key, value):

map = Map.put(map, :d, 4) #=> %{a: 1, b: 2, c: 3, d: 4} 

Remove from Map

Use Map.delete(map, key):

map = Map.delete(map, :b) #=> %{a: 1, c: 3} 
like image 104
Sheharyar Avatar answered Sep 28 '22 03:09

Sheharyar