MutableOrderedScatterSet


public final class MutableOrderedScatterSet<E extends Object> extends OrderedScatterSet


MutableOrderedScatterSet is a container with a MutableSet-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 asMutableSet.

This implementation is not thread-safe: if multiple threads access this container concurrently, and one or more threads modify the structure of the set (insertion or removal for instance), the calling code must provide the appropriate synchronization. Concurrent reads are however safe.

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

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

See also
Set

Summary

Public constructors

<E extends Object> MutableOrderedScatterSet(int initialCapacity)

Creates a new MutableOrderedScatterSet

Public methods

final boolean
add(@NonNull E element)

Adds the specified element to the set.

final boolean
addAll(@NonNull E[] elements)

Adds all the elements into this set.

final boolean
addAll(@NonNull Iterable<@NonNull E> elements)

Adds all the elements into this set.

final boolean

Adds all the elements in the elements set into this set.

final boolean

Adds all the elements in the elements set into this set.

final boolean

Adds all the elements in the elements set into this set.

final boolean
addAll(@NonNull Sequence<@NonNull E> elements)

Adds all the elements into this set.

final @NonNull Set<@NonNull E>

Wraps this OrderedScatterSet with a MutableSet interface.

final void

Removes all elements from this set.

final void
minusAssign(@NonNull E element)

Removes the specified element from the set if it is present.

final void
minusAssign(@NonNull E[] elements)

Removes the specified elements from the set, if present.

final void

Removes the specified elements from the set, if present.

final void

Removes the specified elements from the set, if present.

final void

Removes the specified elements from the set, if present.

final void

Removes the specified elements from the set, if present.

final void

Removes the specified elements from the set, if present.

final void
plusAssign(@NonNull E element)

Adds the specified element to the set.

final void
plusAssign(@NonNull E[] elements)

Adds all the elements into this set.

final void

Adds all the elements into this set.

final void

Adds all the elements in the elements set into this set.

final void

Adds all the elements in the elements set into this set.

final void

Adds all the elements in the elements set into this set.

final void

Adds all the elements into this set.

final boolean
remove(@NonNull E element)

Removes the specified element from the set.

final boolean
removeAll(@NonNull E[] elements)

Removes the specified elements from the set, if present.

final boolean

Removes the specified elements from the set, if present.

final boolean

Removes the specified elements from the set, if present.

final boolean

Removes the specified elements from the set, if present.

final boolean

Removes the specified elements from the set, if present.

final boolean

Removes the specified elements from the set, if present.

final void
removeIf(@NonNull Function1<@NonNull E, @NonNull Boolean> predicate)

Removes any values for which the specified predicate returns true.

final boolean

Removes all the entries in this set that are not contained in elements.

final boolean

Removes all the entries in this set that are not contained in elements.

final boolean

Removes all the entries in this set that are not contained in elements.

final boolean
retainAll(@NonNull Function1<@NonNull E, @NonNull Boolean> predicate)

Removes all the elements in this set for which the specified predicate is true.

final @IntRange(from = 0) int

Trims this MutableOrderedScatterSet's storage so it is sized appropriately to hold the current elements.

final void
trimToSize(int maxSize)

Remove entries until the total size of the remaining entries is less than or equal to maxSize.

Inherited methods

From androidx.collection.OrderedScatterSet
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.

Public constructors

MutableOrderedScatterSet

public <E extends Object> MutableOrderedScatterSet(int initialCapacity)

Creates a new MutableOrderedScatterSet

Parameters
int initialCapacity

The initial desired capacity for this container. the container will honor this value by guaranteeing its internal structures can hold that many elements without requiring any allocations. The initial capacity can be set to 0.

Public methods

add

Added in 1.5.0-alpha01
public final boolean add(@NonNull E element)

Adds the specified element to the set.

Parameters
@NonNull E element

The element to add to the set.

Returns
boolean

true if the element has been added or false if the element is already contained within the set.

addAll

Added in 1.5.0-alpha01
public final boolean addAll(@NonNull E[] elements)

Adds all the elements into this set.

Parameters
@NonNull E[] elements

An array of elements to add to the set.

Returns
boolean

true if any of the specified elements were added to the collection, false if the collection was not modified.

addAll

public final boolean addAll(@NonNull Iterable<@NonNull E> elements)

Adds all the elements into this set.

Parameters
@NonNull Iterable<@NonNull E> elements

Iterable elements to add to the set.

Returns
boolean

true if any of the specified elements were added to the collection, false if the collection was not modified.

addAll

Added in 1.5.0-alpha01
public final boolean addAll(@NonNull ObjectList<@NonNull E> elements)

Adds all the elements in the elements set into this set.

Parameters
@NonNull ObjectList<@NonNull E> elements

An ObjectList whose elements are to be added to the set

Returns
boolean

true if any of the specified elements were added to the collection, false if the collection was not modified.

addAll

Added in 1.5.0-alpha01
public final boolean addAll(@NonNull OrderedScatterSet<@NonNull E> elements)

Adds all the elements in the elements set into this set.

Parameters
@NonNull OrderedScatterSet<@NonNull E> elements

A OrderedScatterSet whose elements are to be added to the set

Returns
boolean

true if any of the specified elements were added to the collection, false if the collection was not modified.

addAll

Added in 1.5.0-alpha01
public final boolean addAll(@NonNull ScatterSet<@NonNull E> elements)

Adds all the elements in the elements set into this set.

Parameters
@NonNull ScatterSet<@NonNull E> elements

A ScatterSet whose elements are to be added to the set

Returns
boolean

true if any of the specified elements were added to the collection, false if the collection was not modified.

addAll

public final boolean addAll(@NonNull Sequence<@NonNull E> elements)

Adds all the elements into this set.

Parameters
@NonNull Sequence<@NonNull E> elements

The sequence of elements to add to the set.

Returns
boolean

true if any of the specified elements were added to the collection, false if the collection was not modified.

asMutableSet

Added in 1.5.0-alpha01
public final @NonNull Set<@NonNull E> asMutableSet()

Wraps this OrderedScatterSet with a MutableSet interface. The MutableSet is backed by the OrderedScatterSet, so changes to the OrderedScatterSet are reflected in the MutableSet and vice-versa. If the OrderedScatterSet is modified while an iteration over the MutableSet is in progress (and vice- versa), the results of the iteration are undefined.

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

clear

Added in 1.5.0-alpha01
public final void clear()

Removes all elements from this set.

minusAssign

Added in 1.5.0-alpha01
public final void minusAssign(@NonNull E element)

Removes the specified element from the set if it is present.

Parameters
@NonNull E element

The element to be removed from the set.

minusAssign

Added in 1.5.0-alpha01
public final void minusAssign(@NonNull E[] elements)

Removes the specified elements from the set, if present.

Parameters
@NonNull E[] elements

An array of elements to be removed from the set.

minusAssign

public final void minusAssign(@NonNull Iterable<@NonNull E> elements)

Removes the specified elements from the set, if present.

Parameters
@NonNull Iterable<@NonNull E> elements

A Iterable of elements to be removed from the set.

minusAssign

Added in 1.5.0-alpha01
public final void minusAssign(@NonNull ObjectList<@NonNull E> elements)

Removes the specified elements from the set, if present.

Parameters
@NonNull ObjectList<@NonNull E> elements

An ObjectList whose elements should be removed from the set.

minusAssign

Added in 1.5.0-alpha01
public final void minusAssign(@NonNull OrderedScatterSet<@NonNull E> elements)

Removes the specified elements from the set, if present.

Parameters
@NonNull OrderedScatterSet<@NonNull E> elements

A OrderedScatterSet whose elements should be removed from the set.

minusAssign

Added in 1.5.0-alpha01
public final void minusAssign(@NonNull ScatterSet<@NonNull E> elements)

Removes the specified elements from the set, if present.

Parameters
@NonNull ScatterSet<@NonNull E> elements

A ScatterSet whose elements should be removed from the set.

minusAssign

public final void minusAssign(@NonNull Sequence<@NonNull E> elements)

Removes the specified elements from the set, if present.

Parameters
@NonNull Sequence<@NonNull E> elements

A sequence of elements to be removed from the set.

plusAssign

Added in 1.5.0-alpha01
public final void plusAssign(@NonNull E element)

Adds the specified element to the set.

Parameters
@NonNull E element

The element to add to the set.

plusAssign

Added in 1.5.0-alpha01
public final void plusAssign(@NonNull E[] elements)

Adds all the elements into this set.

Parameters
@NonNull E[] elements

An array of elements to add to the set.

plusAssign

public final void plusAssign(@NonNull Iterable<@NonNull E> elements)

Adds all the elements into this set.

Parameters
@NonNull Iterable<@NonNull E> elements

Iterable elements to add to the set.

plusAssign

Added in 1.5.0-alpha01
public final void plusAssign(@NonNull ObjectList<@NonNull E> elements)

Adds all the elements in the elements set into this set.

Parameters
@NonNull ObjectList<@NonNull E> elements

An ObjectList whose elements are to be added to the set

plusAssign

Added in 1.5.0-alpha01
public final void plusAssign(@NonNull OrderedScatterSet<@NonNull E> elements)

Adds all the elements in the elements set into this set.

Parameters
@NonNull OrderedScatterSet<@NonNull E> elements

A OrderedScatterSet whose elements are to be added to the set

plusAssign

Added in 1.5.0-alpha01
public final void plusAssign(@NonNull ScatterSet<@NonNull E> elements)

Adds all the elements in the elements set into this set.

Parameters
@NonNull ScatterSet<@NonNull E> elements

A ScatterSet whose elements are to be added to the set

plusAssign

public final void plusAssign(@NonNull Sequence<@NonNull E> elements)

Adds all the elements into this set.

Parameters
@NonNull Sequence<@NonNull E> elements

The sequence of elements to add to the set.

remove

Added in 1.5.0-alpha01
public final boolean remove(@NonNull E element)

Removes the specified element from the set.

Parameters
@NonNull E element

The element to be removed from the set.

Returns
boolean

true if the element was present in the set, or false if it wasn't present before removal.

removeAll

Added in 1.5.0-alpha01
public final boolean removeAll(@NonNull E[] elements)

Removes the specified elements from the set, if present.

Parameters
@NonNull E[] elements

An array of elements to be removed from the set.

Returns
boolean

true if the set was changed or false if none of the elements were present.

removeAll

public final boolean removeAll(@NonNull Iterable<@NonNull E> elements)

Removes the specified elements from the set, if present.

Parameters
@NonNull Iterable<@NonNull E> elements

A Iterable of elements to be removed from the set.

Returns
boolean

true if the set was changed or false if none of the elements were present.

removeAll

Added in 1.5.0-alpha01
public final boolean removeAll(@NonNull ObjectList<@NonNull E> elements)

Removes the specified elements from the set, if present.

Parameters
@NonNull ObjectList<@NonNull E> elements

An ObjectList whose elements should be removed from the set.

Returns
boolean

true if the set was changed or false if none of the elements were present.

removeAll

Added in 1.5.0-alpha01
public final boolean removeAll(@NonNull OrderedScatterSet<@NonNull E> elements)

Removes the specified elements from the set, if present.

Parameters
@NonNull OrderedScatterSet<@NonNull E> elements

A OrderedScatterSet whose elements should be removed from the set.

Returns
boolean

true if the set was changed or false if none of the elements were present.

removeAll

Added in 1.5.0-alpha01
public final boolean removeAll(@NonNull ScatterSet<@NonNull E> elements)

Removes the specified elements from the set, if present.

Parameters
@NonNull ScatterSet<@NonNull E> elements

A ScatterSet whose elements should be removed from the set.

Returns
boolean

true if the set was changed or false if none of the elements were present.

removeAll

public final boolean removeAll(@NonNull Sequence<@NonNull E> elements)

Removes the specified elements from the set, if present.

Parameters
@NonNull Sequence<@NonNull E> elements

A sequence of elements to be removed from the set.

Returns
boolean

true if the set was changed or false if none of the elements were present.

removeIf

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

Removes any values for which the specified predicate returns true.

retainAll

public final boolean retainAll(@NonNull Collection<@NonNull E> elements)

Removes all the entries in this set that are not contained in elements.

Parameters
@NonNull Collection<@NonNull E> elements

A collection of elements to preserve in this set.

Returns
boolean

true if this set was modified, false otherwise.

retainAll

Added in 1.5.0-alpha01
public final boolean retainAll(@NonNull OrderedScatterSet<@NonNull E> elements)

Removes all the entries in this set that are not contained in elements.

elements A set of elements to preserve in this set.

Returns
boolean

true if this set was modified, false otherwise.

retainAll

Added in 1.5.0-alpha01
public final boolean retainAll(@NonNull ScatterSet<@NonNull E> elements)

Removes all the entries in this set that are not contained in elements.

elements A set of elements to preserve in this set.

Returns
boolean

true if this set was modified, false otherwise.

retainAll

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

Removes all the elements in this set for which the specified predicate is true. For each element in the set, the predicate is invoked with that element as the sole parameter.

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

Predicate invoked for each element in the set. When the predicate returns true, the element is kept in the set, otherwise it is removed.

Returns
boolean

true if this set was modified, false otherwise.

trim

Added in 1.5.0-alpha01
public final @IntRange(from = 0) int trim()

Trims this MutableOrderedScatterSet's storage so it is sized appropriately to hold the current elements.

Returns the number of empty elements removed from this set's storage. Returns 0 if no trimming is necessary or possible.

trimToSize

Added in 1.5.0-alpha01
public final void trimToSize(int maxSize)

Remove entries until the total size of the remaining entries is less than or equal to maxSize. Entries added last are removed first. Calling this method has no effect if maxSize is greater than size.