HH\Map
Map
is an ordered dictionary-style collection
HHVM provides a native implementation for this class. The PHP class definition below is not actually used at run time; it is simply provided for the typechecker and for developer reference.
Like all objects in PHP, Map
s have reference-like semantics. When a caller
passes a Map
to a callee, the callee can modify the Map
and the caller
will see the changes. Map
s do not have "copy-on-write" semantics.
Map
s preserve insertion order of key/value pairs. When iterating over a
Map
, the key/value pairs appear in the order they were inserted. Also,
Map
s do not automagically convert integer-like string
keys (ex. "123"
)
into integer keys.
Map
s only support int
keys and string
keys. If a key of a different
type is used, an exception will be thrown.
Map
s support $m[$k]
style syntax for getting and setting values by key.
Map
s also support isset($m[$k])
and empty($m[$k])
syntax, and they
provide similar semantics as arrays. Adding an element with square bracket
syntax []
is supported either by providing a key between the brackets or
a Pair
on the right-hand side. e.g.,
$m[$k] = $v
is supported
$m[] = Pair {$k, $v}
is supported
$m[] = $v
is not supported.
Map
s do not support iterating while new keys are being added or elements
are being removed. When a new key is added or an element is removed, all
iterators that point to the Map
shall be considered invalid.
Guides
Interface Synopsis
namespace HH;
final class Map implements \MutableMap<Tk, Tv> {...}
Public Methods
::fromArray(darray<Tk, Tv> $arr): Map<Tk, Tv>
Returns aMap
containing the key/value pairs from the specifiedarray
::fromItems(?Traversable<Pair<Tk, Tv>> $iterable): Map<Tk, Tv>
Creates aMap
from the givenTraversable
, or an emptyMap
ifnull
is passed->__construct(?KeyedTraversable<Tk, Tv> $iterable = NULL): void
Creates aMap
from the givenKeyedTraversable
, or an emptyMap
ifnull
is passed->__toString(): string
Returns thestring
version of the currentMap
, which is"Map"
->add(Pair<Tk, Tv> $val): Map<Tk, Tv>
Add a key/value pair to the end of the currentMap
->addAll(?Traversable<Pair<Tk, Tv>> $iterable): Map<Tk, Tv>
For every element in the providedTraversable
, add a key/value pair into the currentMap
->at(Tk $key): Tv
Returns the value at the specified key in the currentMap
->clear(): Map<Tk, Tv>
Remove all the elements from the currentMap
->concat<Tu super Tv>(Traversable<Tu> $traversable): Vector<Tu>
Returns aVector
that is the concatenation of the values of the currentMap
and the values of the providedTraversable
->contains(mixed $key): bool
Determines if the specified key is in the currentMap
->containsKey(mixed $key): bool
Determines if the specified key is in the currentMap
->count(): int
Provides the number of elements in the currentMap
->differenceByKey(KeyedTraversable<Tk, Tv> $traversable): Map<Tk, Tv>
Returns a newMap
with the keys that are in the currentMap
, but not in the providedKeyedTraversable
->filter((function(Tv): bool) $callback): Map<Tk, Tv>
Returns aMap
containing the values of the currentMap
that meet a supplied condition->filterWithKey((function(Tk, Tv): bool) $callback): Map<Tk, Tv>
Returns aMap
containing the values of the currentMap
that meet a supplied condition applied to its keys and values->firstKey(): ?Tk
Returns the first key in the currentMap
->firstValue(): ?Tv
Returns the first value in the currentMap
->get(Tk $key): ?Tv
Returns the value at the specified key in the currentMap
->getIterator(): KeyedIterator<Tk, Tv>
Returns an iterator that points to beginning of the currentMap
->immutable(): ImmMap<Tk, Tv>
Returns a deep, immutable copy (ImmMap
) of thisMap
->isEmpty(): bool
Checks if the currentMap
is empty->items(): Iterable<Pair<Tk, Tv>>
Returns anIterable
view of the currentMap
->keys(): Vector<Tk>
Returns aVector
containing the keys of the currentMap
->lastKey(): ?Tk
Returns the last key in the currentMap
->lastValue(): ?Tv
Returns the last value in the currentMap
->lazy(): KeyedIterable<Tk, Tv>
Returns a lazy, access elements only when needed view of the currentMap
->map<Tu>((function(Tv): Tu) $callback): Map<Tk, Tu>
Returns aMap
after an operation has been applied to each value in the currentMap
->mapWithKey<Tu>((function(Tk, Tv): Tu) $callback): Map<Tk, Tu>
Returns aMap
after an operation has been applied to each key and value in the currentMap
->remove(Tk $key): Map<Tk, Tv>
Removes the specified key (and associated value) from the currentMap
->removeKey(Tk $key): Map<Tk, Tv>
Removes the specified key (and associated value) from the currentMap
->reserve(int $sz): void
Reserves enough memory to accommodate a given number of elements->retain(mixed $callback): Map
Ensures that this Map contains only keys/values for which the specified callback returns true when passed the value->retainWithKey(mixed $callback): Map
Ensures that this Map contains only keys/values for which the specified callback returns true when passed the key and the value->set(Tk $key, Tv $value): Map<Tk, Tv>
Stores a value into the currentMap
with the specified key, overwriting the previous value associated with the key->setAll(?KeyedTraversable<Tk, Tv> $iterable): Map<Tk, Tv>
For every element in the providedTraversable
, stores a value into the currentMap
associated with each key, overwriting the previous value associated with the key->skip(int $n): Map<Tk, Tv>
Returns aMap
containing the values after then
-th element of the currentMap
->skipWhile((function(Tv): bool) $fn): Map<Tk, Tv>
Returns aMap
containing the values of the currentMap
starting after and including the first value that producestrue
when passed to the specified callback->slice(int $start, int $len): Map<Tk, Tv>
Returns a subset of the currentMap
starting from a given key location up to, but not including, the element at the provided length from the starting key location->take(int $n): Map<Tk, Tv>
Returns aMap
containing the firstn
key/values of the currentMap
->takeWhile((function(Tv): bool) $callback): Map<Tk, Tv>
Returns aMap
containing the keys and values of the currentMap
up to but not including the first value that producesfalse
when passed to the specified callback->toDArray(): darray<Tk, Tv>
Returns a darray built from the keys and values from this Map->toImmMap(): ImmMap<Tk, Tv>
Returns a deep, immutable copy (ImmMap
) of the currentMap
->toImmSet(): ImmSet<Tv>
Returns an immutable set (ImmSet
) based on the values of the currentMap
->toImmVector(): ImmVector<Tv>
Returns an immutable vector (ImmVector
) with the values of the currentMap
->toKeysArray(): varray<Tk>
Returns anarray
whose values are the keys of the currentMap
->toMap(): Map<Tk, Tv>
Returns a deep copy of the currentMap
->toSet(): Set<Tv>
Returns aSet
based on the values of the currentMap
->toVArray(): varray<Tv>
->toValuesArray(): varray<Tv>
Returns anarray
containing the values from the currentMap
->toVector(): Vector<Tv>
Returns aVector
with the values of the currentMap
->values(): Vector<Tv>
Returns aVector
containing the values of the currentMap
->zip<Tu>(Traversable<Tu> $traversable): Map<Tk, Pair<Tv, Tu>>
Returns aMap
where each value is aPair
that combines the value of the currentMap
and the providedTraversable