# The Map class

(PECL ds >= 1.0.0)

## 简介

A Map is a sequential collection of key-value pairs, almost identical to an array used in a similar context. Keys can be any type, but must be unique. Values are replaced if added to the map using the same key.

## Strengths

- Keys and values can be any type, including objects.
- Supports array syntax (square brackets).
- Insertion order is preserved.
- Performance and memory efficiency is very similar to an array.
- Automatically frees allocated memory when its size drops low enough.

## Weaknesses

- Can’t be converted to an array when objects are used as keys.

## 类摘要

class Ds\Map implements Ds\Collection, ArrayAccess {
/* 常量 */
const int MIN_CAPACITY = 8;
/* 方法 */
public function allocate(int `$capacity`): void
public function apply(callable `$callback`): void
public function capacity(): int
public function clear(): void
public function copy(): Ds\Map
public function diff(Ds\Map `$map`): Ds\Map
public function filter(callable `$callback` = ?): Ds\Map
public function first(): Ds\Pair
public function get(mixed `$key`, mixed `$default` = ?): mixed
public function hasKey(mixed `$key`): bool
public function hasValue(mixed `$value`): bool
public function intersect(Ds\Map `$map`): Ds\Map
public function isEmpty(): bool
public function keys(): Ds\Set
public function ksort(callable `$comparator` = ?): void
public function ksorted(callable `$comparator` = ?): Ds\Map
public function last(): Ds\Pair
public function map(callable `$callback`): Ds\Map
public function merge(mixed `$values`): Ds\Map
public function pairs(): Ds\Sequence
public function put(mixed `$key`, mixed `$value`): void
public function putAll(mixed `$pairs`): void
public function reduce(callable `$callback`, mixed `$initial` = ?): mixed
public function remove(mixed `$key`, mixed `$default` = ?): mixed
public function reverse(): void
public function reversed(): Ds\Map
public function skip(int `$position`): Ds\Pair
public function slice(int `$index`, int `$length` = ?): Ds\Map
public function sort(callable `$comparator` = ?): void
public function sorted(callable `$comparator` = ?): Ds\Map
public function sum(): int|float
public function toArray(): array
public function union(Ds\Map `$map`): Ds\Map
public function values(): Ds\Sequence
public function xor(Ds\Map `$map`): Ds\Map
}

## 预定义常量

`Ds\Map::MIN_CAPACITY`

## 更新日志

版本 | 说明
--- | ---
PECL ds 1.3.0 | The class now implements ArrayAccess.
PECL ds 1.2.0 | `Ds\Map::MIN_CAPACITY` changed from 16 to 8.

## 目录

- Ds\Map::allocate — Allocates enough memory for a required capacity
- Ds\Map::apply — Updates all values by applying a callback function to each value
- Ds\Map::capacity — Returns the current capacity
- Ds\Map::clear — Removes all values
- Ds\Map::__construct — Creates a new instance
- Ds\Map::copy — Returns a shallow copy of the map
- Ds\Map::count — Returns the number of values in the map
- Ds\Map::diff — Creates a new map using keys that aren't in another map
- Ds\Map::filter — Creates a new map using a callable to determine which pairs to include
- Ds\Map::first — Returns the first pair in the map
- Ds\Map::get — Returns the value for a given key
- Ds\Map::hasKey — Determines whether the map contains a given key
- Ds\Map::hasValue — Determines whether the map contains a given value
- Ds\Map::intersect — Creates a new map by intersecting keys with another map
- Ds\Map::isEmpty — Returns whether the map is empty
- Ds\Map::jsonSerialize — Returns a representation that can be converted to JSON
- Ds\Map::keys — Returns a set of the map's keys
- Ds\Map::ksort — Sorts the map in-place by key
- Ds\Map::ksorted — Returns a copy, sorted by key
- Ds\Map::last — Returns the last pair of the map
- Ds\Map::map — Returns the result of applying a callback to each value
- Ds\Map::merge — Returns the result of adding all given associations
- Ds\Map::pairs — Returns a sequence containing all the pairs of the map
- Ds\Map::put — Associates a key with a value
- Ds\Map::putAll — Associates all key-value pairs of a traversable object or array
- Ds\Map::reduce — Reduces the map to a single value using a callback function
- Ds\Map::remove — Removes and returns a value by key
- Ds\Map::reverse — Reverses the map in-place
- Ds\Map::reversed — Returns a reversed copy
- Ds\Map::skip — Returns the pair at a given positional index
- Ds\Map::slice — Returns a subset of the map defined by a starting index and length
- Ds\Map::sort — Sorts the map in-place by value
- Ds\Map::sorted — Returns a copy, sorted by value
- Ds\Map::sum — Returns the sum of all values in the map
- Ds\Map::toArray — Converts the map to an array
- Ds\Map::union — Creates a new map using values from the current instance and another map
- Ds\Map::values — Returns a sequence of the map's values
- Ds\Map::xor — Creates a new map using keys of either the current instance or of another map, but not of both

`↑` and `↓` to navigate • `Enter` to select • `Esc` to close • `/` to open