Map
Map is a specific collection. It differs slightly from the other collections because it consists not only of the value (V) but also of the key (K). The current implementation allows only string keys.
#
ConstructionYou can also create empty map and put values to it using put
method:
Remember that put
will not modify Map
, instead of it will return new Map
instance with one more key.
#
Value extractionTo get value for given key use get
method. It will return Option
object which will be empty for non-existing key.
#
MethodsMap contains couple specific methods (in addition to those normally available for any Traverasable
):
put(string $key, $value): self
- add value for given key and returns new map instance (if key exist value will be replaces with newer one)remove(string $key): self
- removes existing key and returns new map instance (or same if key not exists)mapKeys(callable $keyMapper): self
- maps all keys with given $keyMappermapValues(callable $valueMapper): self
- maps all values with given $valueMappermerge(self $map): self
- merge with other map, if collisions occur, the value of this map is takencontainsKey(string $key): bool
containsValue($value): bool
#
DifferencesUnlike other traversables, the Map
behaves differently for several methods.
contains($element): bool
method will search for Tuple of key and value:
head()
and tail()
will return first and last tuple of key and value:
map()
and other iterable extension with callable
as argument will receive Tuple
as argument.