HH\Map::setAll

For every element in the provided Traversable, stores a value into the current Map associated with each key, overwriting the previous value associated with the key.

Description

public function setAll(
  ?KeyedTraversable<Tk,Tv> $it,
): Map<Tk,Tv>

For every element in the provided Traversable, stores a value into the current Map associated with each key, overwriting the previous value associated with the key.

This method is equivalent to Map::addAll(). If a key to set does not exist in the Map that does exist in the Traversable, it is created. This is inconsistent with, for example, the method Vector::setAll() where if a key is not found, an exception is thrown.

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

Parameters

  • ?KeyedTraversable<Tk,Tv> $it

Return Values

  • Map<Tk,Tv> - A shallow copy of the current Map with the updated the values set. The current Map is also updated.

Examples

This example shows how setAll() can be used with any KeyedTraversable:

<?hh

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

$m = Map {
  'red' => '#ff0000',
  'green' => '#00ff00',
  'blue' => '#0000ff',
  'yellow' => '#ffff00',
};

// Set the values at keys 'red' and 'green'
$m->setAll(Map {
  'red' => 'rgb(255, 0, 0)',
  'green' => 'rgb(0, 255, 0)',
});

// Set the values at keys 'blue' and 'yellow' with an associative array
$m->setAll(array(
  'blue' => 'rgb(0, 0, 255)',
  'yellow' => 'rgb(255, 255, 0)',
));

var_dump($m);
Output
object(HH\Map)#1 (4) {
  ["red"]=>
  string(14) "rgb(255, 0, 0)"
  ["green"]=>
  string(14) "rgb(0, 255, 0)"
  ["blue"]=>
  string(14) "rgb(0, 0, 255)"
  ["yellow"]=>
  string(16) "rgb(255, 255, 0)"
}