HH\Map::addAll

For every element in the provided Traversable, add a key/value pair into the current Map.

Description

public function addAll(
  ?Traversable<Pair<Tk,Tv>> $it,
): Map<Tk,Tv>

For every element in the provided Traversable, add a key/value pair into the current Map.

This method is equivalent to Map::setAll(). If a key in the Traversable exists in the Map, then the value associated with that key in the Map is overwritten.

Future changes made to the current Map ARE reflected in the returned Map, and vice-versa.

Parameters

  • ?Traversable<Pair<Tk,Tv>> $it

Return Values

  • Map<Tk,Tv> - A shallow copy of the current Map with the added key/value pair set. This Map is also updated.

Examples

The following example adds a collection of key-value pairs to the Map $m and also adds multiple collections of key-value pairs to $m through chaining. Since Map::addAll() returns a shallow copy of $m itself, you can chain a bunch of addAll() calls together.

<?hh

namespace Hack\UserDocumentation\API\Examples\Map\AddAll;

$m = Map {};

// Add all the key-value pairs in an array
$m->addAll(array(Pair {'red', '#ff0000'}));

// Map::addAll returns the Map so it can be chained
$m->addAll(Vector {
  Pair {'green', '#00ff00'},
  Pair {'blue', '#0000ff'},
})->addall(ImmVector {
  Pair {'yellow', '#ffff00'},
  Pair {'purple', '#663399'},
});

var_dump($m);
Output
object(HH\Map)#1 (5) {
  ["red"]=>
  string(7) "#ff0000"
  ["green"]=>
  string(7) "#00ff00"
  ["blue"]=>
  string(7) "#0000ff"
  ["yellow"]=>
  string(7) "#ffff00"
  ["purple"]=>
  string(7) "#663399"
}