ScatterSet


public sealed class ScatterSet<E extends Object>

Known direct subclasses
MutableScatterSet

MutableScatterSet is a container with a MutableSet-like interface based on a flat hash table implementation.


ScatterSet is a container with a Set-like interface based on a flat hash table implementation. The underlying implementation is designed to avoid all allocations on insertion, removal, retrieval, and iteration. Allocations may still happen on insertion when the underlying storage needs to grow to accommodate newly added elements to the set.

This implementation makes no guarantee as to the order of the elements, nor does it make guarantees that the order remains constant over time. If the order of the elements must be preserved, please refer to OrderedScatterSet.

Though ScatterSet offers a read-only interface, it is always backed by a MutableScatterSet. Read operations alone are thread-safe. However, any mutations done through the backing MutableScatterSet while reading on another thread are not safe and the developer must protect the set from such changes during read operations.

Note: when a Set is absolutely necessary, you can use the method asSet to create a thin wrapper around a ScatterSet. Please refer to asSet for more details and caveats.

Summary

Protected constructors

<E extends Object> ScatterSet()

Public methods

final boolean
all(@NonNull Function1<@NonNull element, @NonNull Boolean> predicate)

Returns true if all elements match the given predicate.

final boolean
any()

Returns true if this set has at least one element.

final boolean
any(@NonNull Function1<@NonNull element, @NonNull Boolean> predicate)

Returns true if at least one element matches the given predicate.

final @NonNull Set<@NonNull E>

Wraps this ScatterSet with a Set interface.

final boolean
contains(@NonNull E element)

Returns true if the specified element is present in this hash set, false otherwise.

final @IntRange(from = 0) int

Returns the number of elements in this set.

final @IntRange(from = 0) int
count(@NonNull Function1<@NonNull element, @NonNull Boolean> predicate)

Returns the number of elements matching the given predicate.

boolean
equals(Object other)

Compares the specified object other with this hash set for equality.

final @NonNull E

Returns the first element in the collection.

final @NonNull E
first(@NonNull Function1<@NonNull element, @NonNull Boolean> predicate)

Returns the first element in the collection for which predicate returns true

final E
firstOrNull(
    @NonNull Function1<@NonNull element, @NonNull Boolean> predicate
)

Returns the first element in the collection for which predicate returns true or null if there are no elements that match predicate.

final void
forEach(@NonNull Function1<@NonNull element, Unit> block)

Iterates over every element stored in this set by invoking the specified block lambda.

final @IntRange(from = 0) int

Returns the number of elements that can be stored in this set without requiring internal storage reallocation.

final @IntRange(from = 0) int

Returns the number of elements in this set.

int

Returns the hash code value for this set.

final boolean

Indicates whether this set is empty.

final boolean

Returns true if this set is not empty.

final @NonNull String
joinToString(
    @NonNull CharSequence separator,
    @NonNull CharSequence prefix,
    @NonNull CharSequence postfix,
    int limit,
    @NonNull CharSequence truncated,
    Function1<@NonNull E, @NonNull CharSequence> transform
)

Creates a String from the elements separated by separator and using prefix before and postfix after, if supplied.

final boolean

Returns true if this set has no elements.

@NonNull String

Returns a string representation of this set.

Protected constructors

ScatterSet

protected <E extends Object> ScatterSet()

Public methods

all

Added in 1.4.0
public final boolean all(@NonNull Function1<@NonNull element, @NonNull Boolean> predicate)

Returns true if all elements match the given predicate. If there are no elements in the set, true is returned.

Parameters
@NonNull Function1<@NonNull element, @NonNull Boolean> predicate

called for elements in the set to determine if it returns return true for all elements.

any

Added in 1.4.0
public final boolean any()

Returns true if this set has at least one element.

any

Added in 1.4.0
public final boolean any(@NonNull Function1<@NonNull element, @NonNull Boolean> predicate)

Returns true if at least one element matches the given predicate.

Parameters
@NonNull Function1<@NonNull element, @NonNull Boolean> predicate

called for elements in the set to determine if it returns true for any elements.

asSet

Added in 1.4.0
public final @NonNull Set<@NonNull E> asSet()

Wraps this ScatterSet with a Set interface. The Set is backed by the ScatterSet, so changes to the ScatterSet are reflected in the Set. If the ScatterSet is modified while an iteration over the Set is in progress, the results of the iteration are undefined.

Note: while this method is useful to use this ScatterSet with APIs accepting Set interfaces, it is less efficient to do so than to use ScatterSet's APIs directly. While the Set implementation returned by this method tries to be as efficient as possible, the semantics of Set may require the allocation of temporary objects for access and iteration.

contains

Added in 1.4.0
public final boolean contains(@NonNull E element)

Returns true if the specified element is present in this hash set, false otherwise.

Parameters
@NonNull E element

The element to look for in this set

count

Added in 1.4.0
public final @IntRange(from = 0) int count()

Returns the number of elements in this set.

count

Added in 1.4.0
public final @IntRange(from = 0) int count(@NonNull Function1<@NonNull element, @NonNull Boolean> predicate)

Returns the number of elements matching the given predicate.

Parameters
@NonNull Function1<@NonNull element, @NonNull Boolean> predicate

Called for all elements in the set to count the number for which it returns true.

equals

public boolean equals(Object other)

Compares the specified object other with this hash set for equality. The two objects are considered equal if other:

  • Is a ScatterSet

  • Has the same size as this set

  • Contains elements equal to this set's elements

first

Added in 1.4.0
public final @NonNullfirst()

Returns the first element in the collection.

Throws
kotlin.NoSuchElementException

if the collection is empty

first

Added in 1.4.0
public final @NonNullfirst(@NonNull Function1<@NonNull element, @NonNull Boolean> predicate)

Returns the first element in the collection for which predicate returns true

Parameters
@NonNull Function1<@NonNull element, @NonNull Boolean> predicate

called with each element until it returns true.

Returns
@NonNull E

The element for which predicate returns true.

Throws
kotlin.NoSuchElementException

if predicate returns false for all elements or the collection is empty.

firstOrNull

Added in 1.4.0
public final E firstOrNull(
    @NonNull Function1<@NonNull element, @NonNull Boolean> predicate
)

Returns the first element in the collection for which predicate returns true or null if there are no elements that match predicate.

Parameters
@NonNull Function1<@NonNull element, @NonNull Boolean> predicate

called with each element until it returns true.

Returns
E

The element for which predicate returns true or null if there are no elements in the set or predicate returned false for every element in the set.

forEach

Added in 1.4.0
public final void forEach(@NonNull Function1<@NonNull element, Unit> block)

Iterates over every element stored in this set by invoking the specified block lambda. It is safe to remove the element passed to block during iteration.

Parameters
@NonNull Function1<@NonNull element, Unit> block

called with each element in the set

getCapacity

Added in 1.4.0
public final @IntRange(from = 0) int getCapacity()

Returns the number of elements that can be stored in this set without requiring internal storage reallocation.

getSize

Added in 1.4.0
public final @IntRange(from = 0) int getSize()

Returns the number of elements in this set.

hashCode

public int hashCode()

Returns the hash code value for this set. The hash code of a set is based on the sum of the hash codes of the elements in the set, where the hash code of a null element is defined to be zero.

isEmpty

Added in 1.4.0
public final boolean isEmpty()

Indicates whether this set is empty.

isNotEmpty

Added in 1.4.0
public final boolean isNotEmpty()

Returns true if this set is not empty.

joinToString

public final @NonNull String joinToString(
    @NonNull CharSequence separator,
    @NonNull CharSequence prefix,
    @NonNull CharSequence postfix,
    int limit,
    @NonNull CharSequence truncated,
    Function1<@NonNull E, @NonNull CharSequence> transform
)

Creates a String from the elements separated by separator and using prefix before and postfix after, if supplied.

When a non-negative value of limit is provided, a maximum of limit items are used to generate the string. If the collection holds more than limit items, the string is terminated with truncated.

transform may be supplied to convert each element to a custom String.

none

Added in 1.4.0
public final boolean none()

Returns true if this set has no elements.

toString

public @NonNull String toString()

Returns a string representation of this set. The set is denoted in the string by the []. Each element is separated by , .