MutableObjectList


public final class MutableObjectList<E extends Object> extends ObjectList


MutableObjectList is a MutableList-like collection for reference types. It is optimized for fast access, avoiding virtual and interface method access. Methods avoid allocation whenever possible. For example forEach does not need allocate an Iterator.

This implementation is not thread-safe: if multiple threads access this container concurrently, and one or more threads modify the structure of the list (insertion or removal for instance), the calling code must provide the appropriate synchronization. It is also not safe to mutate during reentrancy -- in the middle of a forEach, for example. However, concurrent reads are safe.

Note List access is available through asList when developers need access to the common API.

Note MutableList access is available through asMutableList when developers need access to the common API.

It is best to use this for all internal implementations where a list of reference types is needed. Use MutableList in public API to take advantage of the commonly-used interface. It is common to use MutableObjectList internally and use asMutableList or asList to get a MutableList or List interface for interacting with public APIs.

MutableLongList

Summary

Public constructors

<E extends Object> MutableObjectList(int initialCapacity)

Public methods

final boolean
add(@NonNull E element)

Adds element to the MutableObjectList and returns true.

final void
add(@IntRange(from = 0) int index, @NonNull E element)

Adds element to the MutableObjectList at the given index, shifting over any elements at index and after, if any.

final boolean
addAll(@NonNull E[] elements)

Adds all elements to the end of the MutableObjectList and returns true if the MutableObjectList was changed or false if elements was empty.

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

Adds all elements to the end of the MutableObjectList and returns true if the MutableObjectList was changed or false if elements was empty.

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

Adds all elements to the end of the MutableObjectList and returns true if the MutableObjectList was changed or false if elements was empty.

final boolean

Adds all elements to the end of the MutableObjectList and returns true if the MutableObjectList was changed or false if elements was empty.

final boolean

Adds all elements to the end of the MutableObjectList and returns true if the MutableObjectList was changed or false if elements was empty.

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

Adds all elements to the end of the MutableObjectList and returns true if the MutableObjectList was changed or false if elements was empty.

final boolean
addAll(@IntRange(from = 0) int index, @NonNull E[] elements)

Adds all elements to the MutableObjectList at the given index, shifting over any elements at index and after, if any.

final boolean
addAll(@IntRange(from = 0) int index, @NonNull Collection<@NonNull E> elements)

Adds all elements to the MutableObjectList at the given index, shifting over any elements at index and after, if any.

final boolean
addAll(@IntRange(from = 0) int index, @NonNull ObjectList<@NonNull E> elements)

Adds all elements to the MutableObjectList at the given index, shifting over any elements at index and after, if any.

@NonNull List<@NonNull E>

Returns a List view into the ObjectList.

final @NonNull List<@NonNull E>

Returns a MutableList view into the MutableObjectList.

final void

Removes all elements in the MutableObjectList.

final void
ensureCapacity(int capacity)

Ensures that there is enough space to store capacity elements in the MutableObjectList.

final int

Returns the total number of elements that can be held before the MutableObjectList must grow.

final void
minusAssign(@NonNull E element)

remove from the MutableObjectList

final void
minusAssign(@NonNull E[] elements)

Removes all elements from the MutableObjectList.

final void

Removes all elements from the MutableObjectList.

final void

Removes all elements from the MutableObjectList.

final void

Removes all elements from the MutableObjectList.

final void

Removes all elements from the MutableObjectList.

final void

Removes all elements from the MutableObjectList.

final void
plusAssign(@NonNull E element)

add to the MutableObjectList.

final void
plusAssign(@NonNull E[] elements)

Adds all elements to the end of the MutableObjectList.

final void

Adds all elements to the end of the MutableObjectList.

final void
plusAssign(@NonNull List<@NonNull E> elements)

Adds all elements to the end of the MutableObjectList.

final void

Adds all elements to the end of the MutableObjectList.

final void

Adds all elements to the end of the MutableObjectList.

final void

Adds all elements to the end of the MutableObjectList.

final boolean
remove(@NonNull E element)

Removes element from the MutableObjectList.

final boolean
removeAll(@NonNull E[] elements)

Removes all elements from the MutableObjectList and returns true if anything was removed.

final boolean

Removes all elements from the MutableObjectList and returns true if anything was removed.

final boolean
removeAll(@NonNull List<@NonNull E> elements)

Removes all elements from the MutableObjectList and returns true if anything was removed.

final boolean

Removes all elements from the MutableObjectList and returns true if anything was removed.

final boolean

Removes all elements from the MutableObjectList and returns true if anything was removed.

final boolean

Removes all elements from the MutableObjectList and returns true if anything was removed.

final @NonNull E
removeAt(@IntRange(from = 0) int index)

Removes the element at the given index and returns it.

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

Removes all elements in this list for which predicate returns true.

final void
removeRange(@IntRange(from = 0) int start, @IntRange(from = 0) int end)

Removes elements from index start (inclusive) to end (exclusive).

final boolean
retainAll(@NonNull E[] elements)

Keeps only elements in the MutableObjectList and removes all other values.

final boolean

Keeps only elements in the MutableObjectList and removes all other values.

final boolean

Keeps only elements in the MutableObjectList and removes all other values.

final boolean

Keeps only elements in the MutableObjectList and removes all other values.

final boolean

Keeps only elements in the MutableObjectList and removes all other values.

final @NonNull E
set(@IntRange(from = 0) int index, @NonNull E element)

Sets the value at index to element.

final void
trim(int minCapacity)

Reduces the internal storage.

Inherited methods

From androidx.collection.ObjectList
final boolean
any()

Returns true if there's at least one element in the collection.

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

Returns true if any of the elements give a true return value for predicate.

final boolean
contains(@NonNull E element)

Returns true if the ObjectList contains element or false otherwise.

final boolean
containsAll(@NonNull E[] elements)

Returns true if the ObjectList contains all elements in elements or false if one or more are missing.

final boolean

Returns true if the ObjectList contains all elements in elements or false if one or more are missing.

final boolean

Returns true if the ObjectList contains all elements in elements or false if one or more are missing.

final boolean

Returns true if the ObjectList contains all elements in elements or false if one or more are missing.

final int

Returns the number of elements in this list.

final int
count(@NonNull Function1<@NonNull element, @NonNull Boolean> predicate)

Counts the number of elements matching predicate.

final @NonNull E
elementAt(@IntRange(from = 0) int index)

Returns the element at the given index or throws IndexOutOfBoundsException if the index is out of bounds of this collection.

final @NonNull E
elementAtOrElse(
    @IntRange(from = 0) int index,
    @NonNull Function1<@NonNull Integer, @NonNull E> defaultValue
)

Returns the element at the given index or defaultValue if index is out of bounds of the collection.

boolean
equals(Object other)

Returns true if other is a ObjectList and the contents of this and other are the same.

final @NonNull E

Returns the first element in the ObjectList or throws a NoSuchElementException if it isEmpty.

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

Returns the first element in the ObjectList for which predicate returns true or throws NoSuchElementException if nothing matches.

final E

Returns the first element in the ObjectList or null if it isEmpty.

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

Returns the first element in the ObjectList for which predicate returns true or null if nothing matches.

final @NonNull R
<R extends Object> fold(
    @NonNull R initial,
    @NonNull Function2<@NonNull acc, @NonNull element, @NonNull R> operation
)

Accumulates values, starting with initial, and applying operation to each element in the ObjectList in order.

final @NonNull R
<R extends Object> foldIndexed(
    @NonNull R initial,
    @NonNull Function3<@NonNull Integer, @NonNull acc, @NonNull element, @NonNull R> operation
)

Accumulates values, starting with initial, and applying operation to each element in the ObjectList in order.

final @NonNull R
<R extends Object> foldRight(
    @NonNull R initial,
    @NonNull Function2<@NonNull element, @NonNull acc, @NonNull R> operation
)

Accumulates values, starting with initial, and applying operation to each element in the ObjectList in reverse order.

final @NonNull R
<R extends Object> foldRightIndexed(
    @NonNull R initial,
    @NonNull Function3<@NonNull Integer, @NonNull element, @NonNull acc, @NonNull R> operation
)

Accumulates values, starting with initial, and applying operation to each element in the ObjectList in reverse order.

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

Calls block for each element in the ObjectList, in order.

final void
forEachIndexed(
    @NonNull Function2<@NonNull Integer, @NonNull element, Unit> block
)

Calls block for each element in the ObjectList along with its index, in order.

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

Calls block for each element in the ObjectList in reverse order.

final void
forEachReversedIndexed(
    @NonNull Function2<@NonNull Integer, @NonNull element, Unit> block
)

Calls block for each element in the ObjectList along with its index, in reverse order.

final @NonNull E
get(@IntRange(from = 0) int index)

Returns the element at the given index or throws IndexOutOfBoundsException if the index is out of bounds of this collection.

final @NonNull IntRange

Returns an IntRange of the valid indices for this ObjectList.

final @IntRange(from = -1) int

Returns the last valid index in the ObjectList.

final @IntRange(from = 0) int

The number of elements in the ObjectList.

int

Returns a hash code based on the contents of the ObjectList.

final int
indexOf(@NonNull E element)

Returns the index of element in the ObjectList or -1 if element is not there.

final int
indexOfFirst(
    @NonNull Function1<@NonNull element, @NonNull Boolean> predicate
)

Returns the index if the first element in the ObjectList for which predicate returns true or -1 if there was no element for which predicate returned true.

final int
indexOfLast(
    @NonNull Function1<@NonNull element, @NonNull Boolean> predicate
)

Returns the index if the last element in the ObjectList for which predicate returns true or -1 if there was no element for which predicate returned true.

final boolean

Returns true if the ObjectList has no elements in it or false otherwise.

final boolean

Returns true if there are elements in the ObjectList or false if it is 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 ObjectList or throws a NoSuchElementException if it isEmpty.

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

Returns the last element in the ObjectList for which predicate returns true or throws NoSuchElementException if nothing matches.

final int
lastIndexOf(@NonNull E element)

Returns the index of the last element in the ObjectList that is the same as element or -1 if no elements match.

final E

Returns the last element in the ObjectList or null if it isEmpty.

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

Returns the last element in the ObjectList for which predicate returns true or null if nothing matches.

final boolean

Returns true if the collection has no elements in it.

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

Returns true if any of the elements give a true return value for predicate while iterating in the reverse order.

@NonNull String

Returns a String representation of the list, surrounded by "[]" and each element separated by ", ".

Public constructors

MutableObjectList

public <E extends Object> MutableObjectList(int initialCapacity)

Public methods

add

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

Adds element to the MutableObjectList and returns true.

add

Added in 1.4.0
public final void add(@IntRange(from = 0) int index, @NonNull E element)

Adds element to the MutableObjectList at the given index, shifting over any elements at index and after, if any.

Throws
kotlin.IndexOutOfBoundsException

if index isn't between 0 and size, inclusive

addAll

Added in 1.4.0
public final boolean addAll(@NonNull E[] elements)

Adds all elements to the end of the MutableObjectList and returns true if the MutableObjectList was changed or false if elements was empty.

addAll

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

Adds all elements to the end of the MutableObjectList and returns true if the MutableObjectList was changed or false if elements was empty.

addAll

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

Adds all elements to the end of the MutableObjectList and returns true if the MutableObjectList was changed or false if elements was empty.

addAll

Added in 1.4.0
public final boolean addAll(@NonNull ObjectList<@NonNull E> elements)

Adds all elements to the end of the MutableObjectList and returns true if the MutableObjectList was changed or false if elements was empty.

addAll

Added in 1.4.0
public final boolean addAll(@NonNull ScatterSet<@NonNull E> elements)

Adds all elements to the end of the MutableObjectList and returns true if the MutableObjectList was changed or false if elements was empty.

addAll

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

Adds all elements to the end of the MutableObjectList and returns true if the MutableObjectList was changed or false if elements was empty.

addAll

Added in 1.4.0
public final boolean addAll(@IntRange(from = 0) int index, @NonNull E[] elements)

Adds all elements to the MutableObjectList at the given index, shifting over any elements at index and after, if any.

Returns
boolean

true if the MutableObjectList was changed or false if elements was empty

Throws
kotlin.IndexOutOfBoundsException

if index isn't between 0 and size, inclusive.

addAll

public final boolean addAll(@IntRange(from = 0) int index, @NonNull Collection<@NonNull E> elements)

Adds all elements to the MutableObjectList at the given index, shifting over any elements at index and after, if any.

Returns
boolean

true if the MutableObjectList was changed or false if elements was empty

Throws
kotlin.IndexOutOfBoundsException

if index isn't between 0 and size, inclusive.

addAll

Added in 1.4.0
public final boolean addAll(@IntRange(from = 0) int index, @NonNull ObjectList<@NonNull E> elements)

Adds all elements to the MutableObjectList at the given index, shifting over any elements at index and after, if any.

Returns
boolean

true if the MutableObjectList was changed or false if elements was empty

Throws
kotlin.IndexOutOfBoundsException

if index isn't between 0 and size, inclusive

asList

Added in 1.5.0-beta01
public @NonNull List<@NonNull E> asList()

Returns a List view into the ObjectList. All access to the collection will be less efficient and abides by the allocation requirements of the List. For example, List.forEach will allocate an iterator. All access will go through the more expensive interface calls. Critical performance areas should use the ObjectList API rather than List API, when possible.

asMutableList

Added in 1.4.0
public final @NonNull List<@NonNull E> asMutableList()

Returns a MutableList view into the MutableObjectList. All access to the collection will be less efficient and abides by the allocation requirements of the MutableList. For example, MutableList.forEach will allocate an iterator. All access will go through the more expensive interface calls. Critical performance areas should use the MutableObjectList API rather than MutableList API, when possible.

clear

Added in 1.4.0
public final void clear()

Removes all elements in the MutableObjectList. The storage isn't released.

See also
trim

ensureCapacity

Added in 1.4.0
public final void ensureCapacity(int capacity)

Ensures that there is enough space to store capacity elements in the MutableObjectList.

See also
trim

getCapacity

Added in 1.4.0
public final int getCapacity()

Returns the total number of elements that can be held before the MutableObjectList must grow.

See also
ensureCapacity

minusAssign

Added in 1.4.0
public final void minusAssign(@NonNull E element)

remove from the MutableObjectList

minusAssign

Added in 1.4.0
public final void minusAssign(@NonNull E[] elements)

Removes all elements from the MutableObjectList.

minusAssign

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

Removes all elements from the MutableObjectList.

minusAssign

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

Removes all elements from the MutableObjectList.

minusAssign

Added in 1.4.0
public final void minusAssign(@NonNull ObjectList<@NonNull E> elements)

Removes all elements from the MutableObjectList.

minusAssign

Added in 1.4.0
public final void minusAssign(@NonNull ScatterSet<@NonNull E> elements)

Removes all elements from the MutableObjectList.

minusAssign

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

Removes all elements from the MutableObjectList.

plusAssign

Added in 1.4.0
public final void plusAssign(@NonNull E element)

add to the MutableObjectList.

plusAssign

Added in 1.4.0
public final void plusAssign(@NonNull E[] elements)

Adds all elements to the end of the MutableObjectList.

plusAssign

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

Adds all elements to the end of the MutableObjectList.

plusAssign

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

Adds all elements to the end of the MutableObjectList.

plusAssign

Added in 1.4.0
public final void plusAssign(@NonNull ObjectList<@NonNull E> elements)

Adds all elements to the end of the MutableObjectList.

plusAssign

Added in 1.4.0
public final void plusAssign(@NonNull ScatterSet<@NonNull E> elements)

Adds all elements to the end of the MutableObjectList.

plusAssign

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

Adds all elements to the end of the MutableObjectList.

remove

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

Removes element from the MutableObjectList. If element was in the MutableObjectList and was removed, true will be returned, or false will be returned if the element was not found.

removeAll

Added in 1.4.0
public final boolean removeAll(@NonNull E[] elements)

Removes all elements from the MutableObjectList and returns true if anything was removed.

removeAll

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

Removes all elements from the MutableObjectList and returns true if anything was removed.

removeAll

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

Removes all elements from the MutableObjectList and returns true if anything was removed.

removeAll

Added in 1.4.0
public final boolean removeAll(@NonNull ObjectList<@NonNull E> elements)

Removes all elements from the MutableObjectList and returns true if anything was removed.

removeAll

Added in 1.4.0
public final boolean removeAll(@NonNull ScatterSet<@NonNull E> elements)

Removes all elements from the MutableObjectList and returns true if anything was removed.

removeAll

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

Removes all elements from the MutableObjectList and returns true if anything was removed.

removeAt

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

Removes the element at the given index and returns it.

Throws
kotlin.IndexOutOfBoundsException

if index isn't between 0 and lastIndex, inclusive

removeIf

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

Removes all elements in this list for which predicate returns true.

removeRange

Added in 1.4.0
public final void removeRange(@IntRange(from = 0) int start, @IntRange(from = 0) int end)

Removes elements from index start (inclusive) to end (exclusive).

Throws
kotlin.IndexOutOfBoundsException

if start or end isn't between 0 and size, inclusive

kotlin.IllegalArgumentException

if start is greater than end

retainAll

Added in 1.4.0
public final boolean retainAll(@NonNull E[] elements)

Keeps only elements in the MutableObjectList and removes all other values.

Returns
boolean

true if the MutableObjectList has changed.

retainAll

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

Keeps only elements in the MutableObjectList and removes all other values.

Returns
boolean

true if the MutableObjectList has changed.

retainAll

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

Keeps only elements in the MutableObjectList and removes all other values.

Returns
boolean

true if the MutableObjectList has changed.

retainAll

Added in 1.4.0
public final boolean retainAll(@NonNull ObjectList<@NonNull E> elements)

Keeps only elements in the MutableObjectList and removes all other values.

Returns
boolean

true if the MutableObjectList has changed.

retainAll

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

Keeps only elements in the MutableObjectList and removes all other values.

Returns
boolean

true if the MutableObjectList has changed.

set

Added in 1.4.0
public final @NonNullset(@IntRange(from = 0) int index, @NonNull E element)

Sets the value at index to element.

Returns
@NonNull E

the previous value set at index

Throws
kotlin.IndexOutOfBoundsException

if index isn't between 0 and lastIndex, inclusive

trim

Added in 1.4.0
public final void trim(int minCapacity)

Reduces the internal storage. If capacity is greater than minCapacity and size, the internal storage is reduced to the maximum of size and minCapacity.

See also
ensureCapacity