OrderedScatterSet


public sealed class OrderedScatterSet<E extends Object>

Known direct subclasses
MutableOrderedScatterSet

MutableOrderedScatterSet is a container with a MutableSet-like interface based on a flat hash table implementation that preserves the insertion order for iteration.


OrderedScatterSet is a container with a Set-like interface based on a flat hash table implementation that preserves the insertion order for iteration. 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 guarantees the order of the elements when iterating over them using forEach or the iterator provided by asSet.

Though OrderedScatterSet offers a read-only interface, it is always backed by a MutableOrderedScatterSet. 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 an OrderedScatterSet. Please refer to asSet for more details and caveats.

Summary

Protected constructors

<E extends Object> OrderedScatterSet()

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 OrderedScatterSet 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.The order in which the predicate is called over the elements is arbitrary.

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 insertion order in the collection for which predicate returns true.

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

Returns the first element in insertion order 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 void
forEachReverse(@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 @NonNull E

Returns the last element in the collection.

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

Returns the last element in insertion order in the collection for which predicate returns true.

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

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

final boolean

Returns true if this set has no elements.

final @NonNull List<@NonNull E>

Returns a new list containing this set's entries, in insertion order.

@NonNull String

Returns a string representation of this set.

Protected constructors

OrderedScatterSet

protected <E extends Object> OrderedScatterSet()

Public methods

all

Added in 1.5.0-alpha01
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. The order in which the predicate is called over the elements is arbitrary.

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.5.0-alpha01
public final boolean any()

Returns true if this set has at least one element.

any

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

Returns true if at least one element matches the given predicate. The order in which the predicate is called over the elements is arbitrary.

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.5.0-alpha01
public final @NonNull Set<@NonNull E> asSet()

Wraps this OrderedScatterSet with a Set interface. The Set is backed by the OrderedScatterSet, so changes to the OrderedScatterSet are reflected in the Set. If the OrderedScatterSet 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 OrderedScatterSet with APIs accepting Set interfaces, it is less efficient to do so than to use OrderedScatterSet'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.5.0-alpha01
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.5.0-alpha01
public final @IntRange(from = 0) int count()

Returns the number of elements in this set.

count

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

Returns the number of elements matching the given predicate.The order in which the predicate is called over the elements is arbitrary.

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:

first

Added in 1.5.0-alpha01
public final @NonNullfirst()

Returns the first element in the collection.

Throws
kotlin.NoSuchElementException

if the collection is empty

first

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

Returns the first element in insertion order 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.5.0-alpha01
public final E firstOrNull(
    @NonNull Function1<@NonNull element, @NonNull Boolean> predicate
)

Returns the first element in insertion order 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.5.0-alpha01
public final void forEach(@NonNull Function1<@NonNull element, Unit> block)

Iterates over every element stored in this set by invoking the specified block lambda. The iteration order is the same as the insertion order. 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

forEachReverse

Added in 1.5.0-alpha01
public final void forEachReverse(@NonNull Function1<@NonNull element, Unit> block)

Iterates over every element stored in this set by invoking the specified block lambda. The iteration order is the reverse of the insertion order. 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.5.0-alpha01
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.5.0-alpha01
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.5.0-alpha01
public final boolean isEmpty()

Indicates whether this set is empty.

isNotEmpty

Added in 1.5.0-alpha01
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.

last

Added in 1.5.0-alpha01
public final @NonNulllast()

Returns the last element in the collection.

Throws
kotlin.NoSuchElementException

if the collection is empty

last

Added in 1.5.0-alpha01
public final @NonNulllast(@NonNull Function1<@NonNull element, @NonNull Boolean> predicate)

Returns the last element in insertion order 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.

lastOrNull

Added in 1.5.0-alpha01
public final E lastOrNull(@NonNull Function1<@NonNull element, @NonNull Boolean> predicate)

Returns the first element in insertion order 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.

none

Added in 1.5.0-alpha01
public final boolean none()

Returns true if this set has no elements.

toList

Added in 1.5.0-alpha01
public final @NonNull List<@NonNull E> toList()

Returns a new list containing this set's entries, in insertion order.

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 , .