ObjectList


public sealed class ObjectList<E extends Object>

Known direct subclasses
MutableObjectList

MutableObjectList is a MutableList-like collection for reference types.


ObjectList is a List-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.

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

LongList

Summary

Protected constructors

<E extends Object> ObjectList(int initialCapacity)

Public methods

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.

abstract @NonNull List<@NonNull E>

Returns a List view into the ObjectList.

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

Protected constructors

ObjectList

protected <E extends Object> ObjectList(int initialCapacity)

Public methods

any

Added in 1.4.0
public final boolean any()

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

any

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

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

asList

Added in 1.4.0
public abstract @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.

contains

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

Returns true if the ObjectList contains element or false otherwise.

containsAll

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

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

containsAll

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

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

containsAll

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

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

containsAll

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

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

count

Added in 1.4.0
public final int count()

Returns the number of elements in this list.

count

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

Counts the number of elements matching predicate.

Returns
int

The number of elements in this list for which predicate returns true.

elementAt

Added in 1.4.0
public final @NonNullelementAt(@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.

elementAtOrElse

Added in 1.4.0
public final @NonNullelementAtOrElse(
    @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.

Parameters
index: int

The index of the element whose value should be returned

@NonNull Function1<@NonNull Integer, @NonNull E> defaultValue

A lambda to call with index as a parameter to return a value at an index not in the list.

equals

public boolean equals(Object other)

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

first

Added in 1.4.0
public final @NonNullfirst()

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

first

Added in 1.4.0
public final @NonNullfirst(@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.

firstOrNull

Added in 1.4.0
public final E firstOrNull()

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

firstOrNull

Added in 1.4.0
public 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.

See also
indexOfFirst

fold

Added in 1.4.0
public 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.

Parameters
@NonNull R initial

The value of acc for the first call to operation or return value if there are no elements in this list.

@NonNull Function2<@NonNull acc, @NonNull element, @NonNull R> operation

function that takes current accumulator value and an element, and calculates the next accumulator value.

foldIndexed

Added in 1.4.0
public 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.

foldRight

Added in 1.4.0
public 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.

Parameters
@NonNull R initial

The value of acc for the first call to operation or return value if there are no elements in this list.

@NonNull Function2<@NonNull element, @NonNull acc, @NonNull R> operation

function that takes an element and the current accumulator value, and calculates the next accumulator value.

foldRightIndexed

Added in 1.4.0
public 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.

forEach

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

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

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

will be executed for every element in the list, accepting an element from the list

forEachIndexed

Added in 1.4.0
public 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.

Parameters
@NonNull Function2<@NonNull Integer, @NonNull element, Unit> block

will be executed for every element in the list, accepting the index and the element at that index.

forEachReversed

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

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

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

will be executed for every element in the list, accepting an element from the list

forEachReversedIndexed

Added in 1.4.0
public 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.

Parameters
@NonNull Function2<@NonNull Integer, @NonNull element, Unit> block

will be executed for every element in the list, accepting the index and the element at that index.

get

Added in 1.4.0
public final @NonNullget(@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.

getIndices

Added in 1.4.0
public final @NonNull IntRange getIndices()

Returns an IntRange of the valid indices for this ObjectList.

getLastIndex

Added in 1.4.0
public final @IntRange(from = -1) int getLastIndex()

Returns the last valid index in the ObjectList. This can be -1 when the list is empty.

getSize

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

The number of elements in the ObjectList.

hashCode

public int hashCode()

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

indexOf

Added in 1.4.0
public final int indexOf(@NonNull E element)

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

indexOfFirst

Added in 1.4.0
public 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.

indexOfLast

Added in 1.4.0
public 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.

isEmpty

Added in 1.4.0
public final boolean isEmpty()

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

isNotEmpty

Added in 1.4.0
public final boolean isNotEmpty()

Returns true if there are elements in the ObjectList or false if it is 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.4.0
public final @NonNulllast()

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

last

Added in 1.4.0
public final @NonNulllast(@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.

lastIndexOf

Added in 1.4.0
public 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.

lastOrNull

Added in 1.4.0
public final E lastOrNull()

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

lastOrNull

Added in 1.4.0
public 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.

See also
indexOfLast

none

Added in 1.4.0
public final boolean none()

Returns true if the collection has no elements in it.

reversedAny

Added in 1.4.0
public 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.

toString

public @NonNull String toString()

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