2018-10-12 15:37:13 -04:00
|
|
|
---
|
|
|
|
title: Maps
|
|
|
|
---
|
|
|
|
## Maps
|
|
|
|
|
2019-05-16 20:31:27 +02:00
|
|
|
Maps is the Elixir data structure for key-values.
|
|
|
|
They are not ordered and allow keys of any type.
|
|
|
|
Maps are created using the %{} syntax:
|
2018-10-12 15:37:13 -04:00
|
|
|
|
2019-05-16 20:31:27 +02:00
|
|
|
```
|
|
|
|
iex(1)> %{}
|
|
|
|
%{}
|
|
|
|
iex(2)> %{1 => "one", 2 => "two", 3 => "three"}
|
|
|
|
%{1 => "one", 2 => "two", 3 => "three"}
|
2018-10-12 15:37:13 -04:00
|
|
|
|
2019-05-16 20:31:27 +02:00
|
|
|
```
|
|
|
|
|
|
|
|
Maps can be accessed with `Map.get/3` or `Map.fetch/2` or with through the `map[]` syntax:
|
|
|
|
```
|
|
|
|
iex(1)> map=%{1 => "one", 2 => "two"}
|
|
|
|
%{1 => "one", 2 => "two"}
|
|
|
|
iex(2)> Map.fetch(map, 1)
|
|
|
|
{:ok, "one"}
|
|
|
|
iex(3)> map[2]
|
|
|
|
"two"
|
|
|
|
iex(4)> map[5]
|
|
|
|
nil
|
|
|
|
|
|
|
|
```
|
2018-10-12 15:37:13 -04:00
|
|
|
|
|
|
|
#### More Information:
|
2019-05-16 20:31:27 +02:00
|
|
|
[HexDocs](https://hexdocs.pm/elixir/Map.html)
|