HashSet

class HashSet<T>(initialCapacity: Int = 16) : MutableSet<T> (source)

A mutable hash set containing one element from each group of equal elements.

Elements must retain stable equality and hash codes while stored. Nullable element types may store null, which hashes to zero. Generic floating equality coalesces NaNs and distinguishes positive and negative zero. User equality and hashing callbacks retain their effects and exceptions.

Iteration order is unspecified. Read-only Set aliases observe mutations. Adding a new element, removing an existing element or clearing a non-empty set invalidates existing iterators; adding a duplicate does not. Storage and traversal use ordinary Guest heap and instruction budgets.

Parameters

initialCapacity

requested backing-map bucket capacity, not the initial size or an element limit.

Type Parameters

T

element type.

See also

Throws

if initialCapacity is negative or greater than 1,073,741,824.

Constructors

Link copied to clipboard
constructor(initialCapacity: Int = 16)

creates an empty set with the requested capacity; the default request is 16.

Properties

Link copied to clipboard
open override val isEmpty: Boolean
Link copied to clipboard
open override val size: Int

Number of distinct elements currently stored in this set.

Functions

Link copied to clipboard
open override fun add(element: T): Boolean

Adds element if no equal element is already present.

Link copied to clipboard
abstract fun addAll(p0: Collection<out T?>?): Boolean
Link copied to clipboard
inline fun <T> Iterable<T>.all(predicate: (T) -> Boolean): Boolean

Returns true when every element matches predicate.

Link copied to clipboard
inline fun <T> Iterable<T>.any(predicate: (T) -> Boolean): Boolean

Returns true when at least one element matches predicate.

Link copied to clipboard

Borrows this iterable as a lazy sequence without traversing or copying it. Each traversal requests a new source iterator; later mutations and invalidation follow the iterable's own contract. Wrappers and iterators use ordinary managed heap quotas.

Link copied to clipboard
open override fun clear()

Removes all elements while retaining backing bucket capacity for reuse.

Link copied to clipboard
open operator override fun contains(element: T): Boolean

Checks whether an element equal to element belongs to this set.

Link copied to clipboard
operator fun <T> Iterable<T>.contains(element: T): Boolean

Checks whether this iterable has an element equal to element.

Link copied to clipboard
abstract fun containsAll(p0: Collection<*>?): Boolean
Link copied to clipboard
open operator override fun equals(other: Any?): Boolean

Compares elements using the set's ordering rules and nullable generic equality.

Link copied to clipboard
inline fun <T> Iterable<T>.filter(predicate: (T) -> Boolean): List<T>

Returns a list containing elements matching predicate, in their original iteration order.

Link copied to clipboard

Returns a list of non-null elements in their original iteration order.

Link copied to clipboard
inline fun <T, C : MutableCollection<in T>> Iterable<T>.filterTo(destination: C, predicate: (T) -> Boolean): C

Appends matching elements to destination in iteration order.

Link copied to clipboard
inline fun <T> Iterable<T>.find(predicate: (T) -> Boolean): T?

Returns the first matching element, or null when no element matches predicate.

Link copied to clipboard
fun <T> Iterable<T>.first(): T

Returns the first element, throwing NoSuchElementException when empty.

inline fun <T> Iterable<T>.first(predicate: (T) -> Boolean): T

Returns the first matching element, throwing NoSuchElementException when no element matches.

Link copied to clipboard
fun <T> Iterable<T>.firstOrNull(): T?

Returns the first element, or null when this iterable is empty.

inline fun <T> Iterable<T>.firstOrNull(predicate: (T) -> Boolean): T?

Returns the first matching element, or null when no element matches predicate.

Link copied to clipboard
inline fun <T, R> Iterable<T>.fold(initial: R, operation: (R, T) -> R): R

Accumulates elements in iteration order.

Link copied to clipboard
inline fun <T> Iterable<T>.forEach(action: (T) -> Unit)

Calls action for each element in iteration order.

Link copied to clipboard
inline fun <T> Iterable<T>.forEachIndexed(action: (Int, T) -> Unit)

Calls action for each element and its zero-based index in iteration order.

Link copied to clipboard
open override fun hashCode(): Int

Combines element hashes with ordinary Int overflow; null contributes zero.

Link copied to clipboard
fun <T> Iterable<T>.indexOf(element: T): Int

Returns the index of the first equal element, or -1 when no element matches.

Link copied to clipboard
open override fun isEmpty(): Boolean

Checks whether this set contains no elements.

Link copied to clipboard

Checks whether this collection contains at least one element.

Link copied to clipboard
open operator override fun iterator(): MutableIterator<T>

Creates a new mutable iterator over this set in unspecified order.

Link copied to clipboard
fun <T> Iterable<T>.lastIndexOf(element: T): Int

Returns the index of the last equal element, or -1 when no element matches.

Link copied to clipboard
fun <T> Iterable<T>.lastOrNull(): T?

Returns the last element, or null when this iterable is empty.

inline fun <T> Iterable<T>.lastOrNull(predicate: (T) -> Boolean): T?

Returns the last matching element after traversing this iterable in iteration order.

Link copied to clipboard
inline fun <T, R> Collection<T>.map(transform: (T) -> R): List<R>
inline fun <T, R> Iterable<T>.map(transform: (T) -> R): List<R>

Returns a list containing transform applied once to each element in iteration order.

Link copied to clipboard
inline fun <T, R : Any> Iterable<T>.mapNotNull(transform: (T) -> R?): List<R>

Applies transform once per element in iteration order and retains only non-null results.

Link copied to clipboard
inline fun <T, R : Any, C : MutableCollection<in R>> Iterable<T>.mapNotNullTo(destination: C, transform: (T) -> R?): C

Appends non-null transformed elements to destination in iteration order.

Link copied to clipboard
inline fun <T, R, C : MutableCollection<in R>> Iterable<T>.mapTo(destination: C, transform: (T) -> R): C

Appends transformed elements to destination in iteration order.

Link copied to clipboard
inline fun <T> Iterable<T>.none(predicate: (T) -> Boolean): Boolean

Returns true when no element matches predicate.

Link copied to clipboard
open override fun remove(element: T): Boolean

Removes an element equal to element, if one exists.

Link copied to clipboard
abstract fun removeAll(p0: Collection<*>?): Boolean
Link copied to clipboard
abstract fun retainAll(p0: Collection<*>?): Boolean
Link copied to clipboard

Numeric accumulation traverses once in iteration order without an intermediate collection. Empty iterables return positive numeric zero. Integer arithmetic wraps at the result width; Float/Double addition retains ordinary rounding, infinities and NaN. Byte/Short widen to Int; UByte/UShort widen to UInt. Iterator failures propagate unchanged.

Link copied to clipboard
inline fun <T> Iterable<T>.sumOf(selector: (T) -> Double): Double

Sums selector once per element in iteration order, returning Double zero when empty. No collection is allocated; selector/iterator exceptions and inline non-local returns propagate. Addition retains ordinary Double rounding, infinities and NaN.

inline fun <T> Iterable<T>.sumOf(selector: (T) -> Int): Int

Sums selector once per element in iteration order, returning Int zero when empty. No collection is allocated; selector/iterator exceptions and inline non-local returns propagate. Addition uses ordinary wrapping Int arithmetic.

inline fun <T> Iterable<T>.sumOf(selector: (T) -> Long): Long

Sums selector once per element in iteration order, returning Long zero when empty. No collection is allocated; selector/iterator exceptions and inline non-local returns propagate. Addition uses ordinary wrapping Long arithmetic.

inline fun <T> Iterable<T>.sumOf(selector: (T) -> UInt): UInt

Sums selector once per element in iteration order, returning UInt zero when empty. No collection is allocated; selector/iterator exceptions and inline non-local returns propagate. Addition uses ordinary wrapping UInt arithmetic.

inline fun <T> Iterable<T>.sumOf(selector: (T) -> ULong): ULong

Sums selector once per element in iteration order, returning ULong zero when empty. No collection is allocated; selector/iterator exceptions and inline non-local returns propagate. Addition uses ordinary wrapping ULong arithmetic.

Link copied to clipboard
open override fun toString(): String

Formats elements in brackets; a direct self reference uses (this Collection).