ObjectList
-
Cmn
sealed class ObjectList<E : Any?>
MutableObjectList |
|
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
| See also | |
|---|---|
MutableObjectList |
|
FloatList |
|
IntList |
Summary
Protected constructors |
|
|---|---|
<E : Any?> ObjectList(initialCapacity: Int) |
Cmn
|
Public functions |
||
|---|---|---|
Boolean |
any()Returns |
Cmn
|
inline Boolean |
Returns |
Cmn
|
abstract List<E> |
asList()Returns a |
Cmn
|
operator Boolean |
contains(element: E)Returns |
Cmn
|
Boolean |
containsAll(elements: Array<E>)Returns |
Cmn
|
Boolean |
containsAll(elements: Iterable<E>)Returns |
Cmn
|
Boolean |
containsAll(elements: List<E>)Returns |
Cmn
|
Boolean |
containsAll(elements: ObjectList<E>)Returns |
Cmn
|
Int |
count()Returns the number of elements in this list. |
Cmn
|
inline Int |
Counts the number of elements matching |
Cmn
|
E |
Returns the element at the given |
Cmn
|
inline E |
elementAtOrElse(index: @IntRange(from = 0) Int, defaultValue: (index: Int) -> E)Returns the element at the given |
Cmn
|
open operator Boolean |
Returns |
Cmn
|
E |
first()Returns the first element in the |
Cmn
|
inline E |
Returns the first element in the |
Cmn
|
inline E? |
Returns the first element in the |
Cmn
|
inline E? |
firstOrNull(predicate: (element) -> Boolean)Returns the first element in the |
Cmn
|
inline R |
Accumulates values, starting with |
Cmn
|
inline R |
<R : Any?> foldIndexed(initial: R, operation: (index: Int, acc, element) -> R)Accumulates values, starting with |
Cmn
|
inline R |
Accumulates values, starting with |
Cmn
|
inline R |
<R : Any?> foldRightIndexed(initial: R, operation: (index: Int, element, acc) -> R)Accumulates values, starting with |
Cmn
|
inline Unit |
Calls |
Cmn
|
inline Unit |
forEachIndexed(block: (index: Int, element) -> Unit)Calls |
Cmn
|
inline Unit |
forEachReversed(block: (element) -> Unit)Calls |
Cmn
|
inline Unit |
forEachReversedIndexed(block: (index: Int, element) -> Unit)Calls |
Cmn
|
operator E |
Returns the element at the given |
Cmn
|
open Int |
hashCode()Returns a hash code based on the contents of the |
Cmn
|
Int |
indexOf(element: E)Returns the index of |
Cmn
|
inline Int |
indexOfFirst(predicate: (element) -> Boolean)Returns the index if the first element in the |
Cmn
|
inline Int |
indexOfLast(predicate: (element) -> Boolean)Returns the index if the last element in the |
Cmn
|
Boolean |
isEmpty()Returns |
Cmn
|
Boolean |
Returns |
Cmn
|
String |
joinToString(Creates a String from the elements separated by |
Cmn
|
E |
last()Returns the last element in the |
Cmn
|
inline E |
Returns the last element in the |
Cmn
|
Int |
lastIndexOf(element: E)Returns the index of the last element in the |
Cmn
|
inline E? |
Returns the last element in the |
Cmn
|
inline E? |
lastOrNull(predicate: (element) -> Boolean)Returns the last element in the |
Cmn
|
Boolean |
none()Returns |
Cmn
|
inline Boolean |
reversedAny(predicate: (element) -> Boolean)Returns |
Cmn
|
open String |
toString()Returns a String representation of the list, surrounded by "[]" and each element separated by ", ". |
Cmn
|
Public properties |
||
|---|---|---|
IntRange |
Returns an |
Cmn
|
Int |
Returns the last valid index in the |
Cmn
|
Int |
The number of elements in the |
Cmn
|
Protected constructors
Public functions
any
inline fun any(predicate: (element) -> Boolean): Boolean
Returns true if any of the elements give a true return value for predicate.
asList
abstract fun asList(): List<E>
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
operator fun contains(element: E): Boolean
Returns true if the ObjectList contains element or false otherwise.
containsAll
fun containsAll(elements: Array<E>): Boolean
Returns true if the ObjectList contains all elements in elements or false if one or more are missing.
containsAll
fun containsAll(elements: Iterable<E>): Boolean
Returns true if the ObjectList contains all elements in elements or false if one or more are missing.
containsAll
fun containsAll(elements: List<E>): Boolean
Returns true if the ObjectList contains all elements in elements or false if one or more are missing.
containsAll
fun containsAll(elements: ObjectList<E>): Boolean
Returns true if the ObjectList contains all elements in elements or false if one or more are missing.
count
inline fun count(predicate: (element) -> Boolean): Int
Counts the number of elements matching predicate.
elementAt
fun elementAt(index: @IntRange(from = 0) Int): E
Returns the element at the given index or throws IndexOutOfBoundsException if the index is out of bounds of this collection.
elementAtOrElse
inline fun elementAtOrElse(index: @IntRange(from = 0) Int, defaultValue: (index: Int) -> E): E
Returns the element at the given index or defaultValue if index is out of bounds of the collection.
equals
open operator fun equals(other: Any?): Boolean
Returns true if other is a ObjectList and the contents of this and other are the same.
first
fun first(): E
Returns the first element in the ObjectList or throws a NoSuchElementException if it isEmpty.
first
inline fun first(predicate: (element) -> Boolean): E
Returns the first element in the ObjectList for which predicate returns true or throws NoSuchElementException if nothing matches.
| See also | |
|---|---|
indexOfFirst |
|
firstOrNull |
firstOrNull
inline fun firstOrNull(): E?
Returns the first element in the ObjectList or null if it isEmpty.
firstOrNull
inline fun firstOrNull(predicate: (element) -> Boolean): E?
Returns the first element in the ObjectList for which predicate returns true or null if nothing matches.
| See also | |
|---|---|
indexOfFirst |
fold
inline fun <R : Any?> fold(initial: R, operation: (acc, element) -> R): R
Accumulates values, starting with initial, and applying operation to each element in the ObjectList in order.
| Parameters | |
|---|---|
initial: R |
The value of |
operation: (acc, element) -> R |
function that takes current accumulator value and an element, and calculates the next accumulator value. |
foldIndexed
inline fun <R : Any?> foldIndexed(initial: R, operation: (index: Int, acc, element) -> R): R
Accumulates values, starting with initial, and applying operation to each element in the ObjectList in order.
foldRight
inline fun <R : Any?> foldRight(initial: R, operation: (element, acc) -> R): R
Accumulates values, starting with initial, and applying operation to each element in the ObjectList in reverse order.
| Parameters | |
|---|---|
initial: R |
The value of |
operation: (element, acc) -> R |
function that takes an element and the current accumulator value, and calculates the next accumulator value. |
foldRightIndexed
inline fun <R : Any?> foldRightIndexed(initial: R, operation: (index: Int, element, acc) -> R): R
Accumulates values, starting with initial, and applying operation to each element in the ObjectList in reverse order.
forEach
inline fun forEach(block: (element) -> Unit): Unit
Calls block for each element in the ObjectList, in order.
| Parameters | |
|---|---|
block: (element) -> Unit |
will be executed for every element in the list, accepting an element from the list |
forEachIndexed
inline fun forEachIndexed(block: (index: Int, element) -> Unit): Unit
Calls block for each element in the ObjectList along with its index, in order.
forEachReversed
inline fun forEachReversed(block: (element) -> Unit): Unit
Calls block for each element in the ObjectList in reverse order.
| Parameters | |
|---|---|
block: (element) -> Unit |
will be executed for every element in the list, accepting an element from the list |
forEachReversedIndexed
inline fun forEachReversedIndexed(block: (index: Int, element) -> Unit): Unit
Calls block for each element in the ObjectList along with its index, in reverse order.
get
operator fun get(index: @IntRange(from = 0) Int): E
Returns the element at the given index or throws IndexOutOfBoundsException if the index is out of bounds of this collection.
indexOf
fun indexOf(element: E): Int
Returns the index of element in the ObjectList or -1 if element is not there.
indexOfFirst
inline fun indexOfFirst(predicate: (element) -> Boolean): Int
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
inline fun indexOfLast(predicate: (element) -> Boolean): Int
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
fun isEmpty(): Boolean
Returns true if the ObjectList has no elements in it or false otherwise.
isNotEmpty
fun isNotEmpty(): Boolean
Returns true if there are elements in the ObjectList or false if it is empty.
joinToString
fun joinToString(
separator: CharSequence = ", ",
prefix: CharSequence = "",
postfix: CharSequence = "",
limit: Int = -1,
truncated: CharSequence = "...",
transform: ((E) -> CharSequence)? = null
): String
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
fun last(): E
Returns the last element in the ObjectList or throws a NoSuchElementException if it isEmpty.
last
inline fun last(predicate: (element) -> Boolean): E
Returns the last element in the ObjectList for which predicate returns true or throws NoSuchElementException if nothing matches.
| See also | |
|---|---|
indexOfLast |
|
lastOrNull |
lastIndexOf
fun lastIndexOf(element: E): Int
Returns the index of the last element in the ObjectList that is the same as element or -1 if no elements match.
lastOrNull
inline fun lastOrNull(): E?
Returns the last element in the ObjectList or null if it isEmpty.
lastOrNull
inline fun lastOrNull(predicate: (element) -> Boolean): E?
Returns the last element in the ObjectList for which predicate returns true or null if nothing matches.
| See also | |
|---|---|
indexOfLast |
reversedAny
inline fun reversedAny(predicate: (element) -> Boolean): Boolean
Returns true if any of the elements give a true return value for predicate while iterating in the reverse order.
Public properties
lastIndex
val lastIndex: Int
Returns the last valid index in the ObjectList. This can be -1 when the list is empty.