hphp/hack/manual/apis/Classes/HH/Set/index.md
:::info[Note] This is a point-in-time snapshot of the API documentation from January 2026. Going forward, we will not be maintaining a public copy of these references, and recommend users to refer to the built-in signature helpers available in the Hack LSP instead for complete and up-to-date information. :::
Set is an ordered set-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, Sets have reference-like semantics. When a caller
passes a Set to a callee, the callee can modify the Set and the caller
will see the changes. Sets do not have "copy-on-write" semantics.
Sets preserve insertion order of the elements. When iterating over a
Set, the elements appear in the order they were inserted. Also, Sets do
not automagically convert integer-like strings (ex. "123") into integers.
Sets only support int values and string values. If a value of a
different type is used, an exception will be thrown.
In general, Sets do not support $c[$k] style syntax. Adding an element
using $c[] = .. syntax is supported.
Set do not support iteration while elements are being added or removed.
When an element is added or removed, all iterators that point to the Set
shall be considered invalid.
More documentation and examples are in the
Set and ImmSet
guide.
namespace HH;
final class Set implements \MutableSet<Tv> {...}
::fromArray(darray<arraykey, Tv> $arr): Set<Tv>Set containing the values from the specified array::fromArrays(...$argv): Set<Tv>Set containing all the values from the specified array(s)::fromItems(?Traversable<Tv> $iterable): Set<Tv>Set from the given Traversable, or an empty Set if null
is passed::fromKeysOf<Tk as arraykey>(?KeyedContainer<Tk, mixed> $container): Set<Tk>Set from the keys of the specified container->__construct(?Traversable<Tv> $iterable = NULL): voidSet from the given Traversable, or an empty Set if null
is passed->__toString(): stringstring version of the current Set, which is "Set"->add(Tv $val): Set<Tv>Set->addAll(?Traversable<Tv> $iterable): Set<Tv>Traversable, add the value into the
current Set->addAllKeysOf(?KeyedContainer<Tv, mixed> $container): Set<Tv>Set as new
values->clear(): Set<Tv>Set->concat<Tu super Tv>(Traversable<Tu> $traversable): Vector<Tu>Vector that is the concatenation of the values of the current
Set and the values of the provided Traversable->contains(arraykey $val): boolSet->count(): intSet->difference(mixed $iterable)->filter((function(Tv): bool) $callback): Set<Tv>Set containing the values of the current Set that meet
a supplied condition applied to each value->filterWithKey((function(arraykey, Tv): bool) $callback): Set<Tv>Set containing the values of the current Set that meet
a supplied condition applied to its "keys" and values->firstKey(): ?arraykeySet->firstValue(): ?TvSet->getIterator(): KeyedIterator<arraykey, Tv>Set->immutable(): ImmSet<Tv>ImmSet), deep copy of the current Set->isEmpty(): boolSet is empty->items(): Iterable<Tv>Iterable view of the current Set->keys(): Vector<arraykey>Vector containing the values of the current Set->lastKey(): ?arraykeySet->lastValue(): ?TvSet->lazy(): KeyedIterable<arraykey, Tv>Set->map<Tu as arraykey>((function(Tv): Tu) $callback): Set<Tu>Set containing the values after an operation has been applied
to each value in the current Set->mapWithKey<Tu as arraykey>((function(arraykey, Tv): Tu) $callback): Set<Tu>Set containing the values after an operation has been applied
to each "key" and value in the current Set->remove(Tv $val): Set<Tv>Set->removeAll(Traversable<Tv> $iterable): Set<Tv>Set that are also in the Traversable->reserve(int $sz): void->retain((function(Tv): bool) $callback): Set<Tv>Set so that it only contains the values that meet a
supplied condition on each value->retainWithKey((function(arraykey, Tv): bool) $callback): Set<Tv>Set so that it only contains the values that meet a
supplied condition on its "keys" and values->skip(int $n): Set<Tv>Set containing the values after the n-th element of the
current Set->skipWhile((function(Tv): bool) $fn): Set<Tv>Set containing the values of the current Set starting after
and including the first value that produces true when passed to the
specified callback->slice(int $start, int $len): Set<Tv>Set starting from a given key up to, but
not including, the element at the provided length from the starting key->take(int $n): Set<Tv>Set containing the first n values of the current Set->takeWhile((function(Tv): bool) $callback): Set<Tv>Set containing the values of the current Set up to but not
including the first value that produces false when passed to the
specified callback->toDArray(): darray<Tv, Tv>->toImmMap(): ImmMap<arraykey, Tv>ImmMap) based on the values of the current
Set->toImmSet(): ImmSet<Tv>ImmSet), deep copy of the current Set->toImmVector(): ImmVector<Tv>ImmVector) with the values of the current
Set->toKeysArray(): varray<Tv>array containing the values from the current Set->toMap(): Map<arraykey, Tv>Map based on the values of the current Set->toSet(): Set<Tv>Set->toVArray(): varray<Tv>->toValuesArray(): varray<Tv>array containing the values from the current Set->toVector(): Vector<Tv>Vector of the current Set values->values(): Vector<Tv>Vector containing the values of the current Set->zip<Tu>(Traversable<Tu> $traversable): Set<nothing>Set or the Traversable is
empty