LongList


public sealed class LongList

Known direct subclasses
MutableLongList

MutableLongList is a MutableList-like collection for Long values.


LongList is a List-like collection for Long values. It allows retrieving the elements without boxing. LongList is always backed by a MutableLongList, its MutableList-like subclass. The purpose of this class is to avoid the performance overhead of auto-boxing due to generics since Collection classes all operate on objects.

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.

Summary

Protected constructors

LongList(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 Long, @NonNull Boolean> predicate)

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

final int
binarySearch(int element, int fromIndex, int toIndex)

Searches this list the specified element in the range defined by fromIndex and toIndex.

final boolean
contains(long element)

Returns true if the LongList contains element or false otherwise.

final boolean

Returns true if the LongList 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 Long, @NonNull Boolean> predicate)

Counts the number of elements matching predicate.

final long
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 long
elementAtOrElse(
    @IntRange(from = 0) int index,
    @NonNull Function1<@NonNull Integer, @NonNull Long> 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 LongList and the contents of this and other are the same.

final long

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

final long
first(@NonNull Function1<@NonNull Long, @NonNull Boolean> predicate)

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

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

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

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

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

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

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

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

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

final void
forEach(@NonNull Function1<@NonNull LongUnit> block)

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

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

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

final void
forEachReversed(@NonNull Function1<@NonNull LongUnit> block)

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

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

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

final long
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 LongList.

final @IntRange(from = -1) int

Returns the last valid index in the LongList.

final @IntRange(from = 0) int

The number of elements in the LongList.

int

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

final int
indexOf(long element)

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

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

Returns the index if the first element in the LongList for which predicate returns true.

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

Returns the index if the last element in the LongList for which predicate returns true.

final boolean

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

final boolean

Returns true if there are elements in the LongList or false if it is empty.

final @NonNull String
joinToString(
    @NonNull CharSequence separator,
    @NonNull CharSequence prefix,
    @NonNull CharSequence postfix,
    int limit,
    @NonNull CharSequence truncated
)

Creates a String from the elements separated by separator and using prefix before and postfix after, if supplied.

final @NonNull String
joinToString(
    @NonNull CharSequence separator,
    @NonNull CharSequence prefix,
    @NonNull CharSequence postfix,
    int limit,
    @NonNull CharSequence truncated,
    @NonNull Function1<@NonNull Long, @NonNull CharSequence> transform
)

Creates a String from the elements separated by separator and using prefix before and postfix after, if supplied.

final long

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

final long
last(@NonNull Function1<@NonNull Long, @NonNull Boolean> predicate)

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

final int
lastIndexOf(long element)

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

final boolean

Returns true if the collection has no elements in it.

final boolean
reversedAny(@NonNull Function1<@NonNull Long, @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

LongList

protected LongList(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 Long, @NonNull Boolean> predicate)

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

binarySearch

Added in 1.5.0-alpha05
public final int binarySearch(int element, int fromIndex, int toIndex)

Searches this list the specified element in the range defined by fromIndex and toIndex. The list is expected to be sorted into ascending order according to the natural ordering of its elements, otherwise the result is undefined.

fromIndex must be >= 0 and <toIndex, and toIndex must be <= size, otherwise an an IndexOutOfBoundsException will be thrown.

Returns
int

the index of the element if it is contained in the list within the specified range. otherwise, the inverted insertion point (-insertionPoint - 1). The insertion point is defined as the index at which the element should be inserted, so that the list remains sorted.

contains

Added in 1.4.0
public final boolean contains(long element)

Returns true if the LongList contains element or false otherwise.

containsAll

Added in 1.4.0
public final boolean containsAll(@NonNull LongList elements)

Returns true if the LongList 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 Long, @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 long 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.

elementAtOrElse

Added in 1.4.0
public final long elementAtOrElse(
    @IntRange(from = 0) int index,
    @NonNull Function1<@NonNull Integer, @NonNull Long> 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 Long> 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 LongList and the contents of this and other are the same.

first

Added in 1.4.0
public final long first()

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

first

Added in 1.4.0
public final long first(@NonNull Function1<@NonNull Long, @NonNull Boolean> predicate)

Returns the first element in the LongList for which predicate returns true or throws NoSuchElementException 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 Long, @NonNull R> operation
)

Accumulates values, starting with initial, and applying operation to each element in the LongList 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 Long, @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 Long, @NonNull R> operation
)

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

foldRight

Added in 1.4.0
public final @NonNull R <R extends Object> foldRight(
    @NonNull R initial,
    @NonNull Function2<@NonNull Long, @NonNull acc, @NonNull R> operation
)

Accumulates values, starting with initial, and applying operation to each element in the LongList 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 Long, @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 Long, @NonNull acc, @NonNull R> operation
)

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

forEach

Added in 1.4.0
public final void forEach(@NonNull Function1<@NonNull LongUnit> block)

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

Parameters
@NonNull Function1<@NonNull LongUnit> 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 LongUnit> block
)

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

Parameters
@NonNull Function2<@NonNull Integer, @NonNull LongUnit> 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 LongUnit> block)

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

Parameters
@NonNull Function1<@NonNull LongUnit> 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 LongUnit> block
)

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

Parameters
@NonNull Function2<@NonNull Integer, @NonNull LongUnit> 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 long 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.

getIndices

Added in 1.4.0
public final @NonNull IntRange getIndices()

Returns an IntRange of the valid indices for this LongList.

getLastIndex

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

Returns the last valid index in the LongList. 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 LongList.

hashCode

public int hashCode()

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

indexOf

Added in 1.4.0
public final int indexOf(long element)

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

indexOfFirst

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

Returns the index if the first element in the LongList for which predicate returns true.

indexOfLast

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

Returns the index if the last element in the LongList for which predicate returns true.

isEmpty

Added in 1.4.0
public final boolean isEmpty()

Returns true if the LongList 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 LongList or false if it is empty.

joinToString

Added in 1.4.0
public final @NonNull String joinToString(
    @NonNull CharSequence separator,
    @NonNull CharSequence prefix,
    @NonNull CharSequence postfix,
    int limit,
    @NonNull CharSequence truncated
)

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.

joinToString

public final @NonNull String joinToString(
    @NonNull CharSequence separator,
    @NonNull CharSequence prefix,
    @NonNull CharSequence postfix,
    int limit,
    @NonNull CharSequence truncated,
    @NonNull Function1<@NonNull Long, @NonNull CharSequence> transform
)

Creates a String from the elements separated by separator and using prefix before and postfix after, if supplied. transform dictates how each element will be represented.

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.

last

Added in 1.4.0
public final long last()

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

last

Added in 1.4.0
public final long last(@NonNull Function1<@NonNull Long, @NonNull Boolean> predicate)

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

See also
indexOfLast

lastIndexOf

Added in 1.4.0
public final int lastIndexOf(long element)

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

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