The Kotlin List
class in the kotlin.collections
package provides a variety of functions to handle lists. Lists in Kotlin are ordered collections of items that allow duplicate elements, and Kotlin provides many utility functions to manipulate lists easily and efficiently.
This guide covers various methods available in the Kotlin List
class. Each method is described in simple terms to help beginners understand how to use them. These methods enable you to perform common operations such as querying, modifying, and transforming lists.
Kotlin List Functions
The table below contains various functions of the Kotlin List
class, each with a link to a detailed explanation, examples, and real-world uses. Click on the method names to learn more about how to use them effectively in your applications.
Method | Description |
---|---|
Kotlin List last Function | Returns the last element of the list. |
Kotlin MutableList reverse Function | Reverses the elements in the list. |
Kotlin List plus Function | Returns a list containing elements of the original list and the given element. |
Kotlin Boolean equals Function | Indicates whether some other object is “equal to” this one. |
Kotlin Boolean compareTo Function | Compares this Boolean with the specified Boolean for order. |
Kotlin Boolean toString Function | Returns a string representation of this Boolean. |
Kotlin Boolean and Function | Performs a logical AND operation between this Boolean and the specified Boolean. |
Kotlin Boolean or Function | Performs a logical OR operation between this Boolean and the specified Boolean. |
Kotlin Boolean not Function | Performs a logical NOT operation on this Boolean. |
Kotlin Boolean xor Function | Performs a logical XOR operation between this Boolean and the specified Boolean. |
Kotlin listOf Function | Returns an immutable list containing the specified elements. |
Kotlin mutableListOf Function | Returns a mutable list containing the specified elements. |
Kotlin arrayListOf Function | Returns an ArrayList containing the specified elements. |
Kotlin emptyList Function | Returns an empty immutable list. |
Kotlin listOfNotNull Function | Returns a list containing all elements that are not null. |
Kotlin List asReversed Function | Returns a reversed view of the original list. |
Kotlin List binarySearch Function | Searches the list for the specified element using the binary search algorithm. |
Kotlin List getOrNull Function | Returns the element at the specified index or null if the index is out of bounds. |
Kotlin List getOrElse Function | Returns the element at the specified index or the result of the given function if the index is out of bounds. |
Kotlin List subList Function | Returns a view of the portion of this list between the specified fromIndex (inclusive) and toIndex (exclusive). |
Kotlin setOf Function | Returns an immutable set containing the specified elements. |
Kotlin mutableSetOf Function | Returns a mutable set containing the specified elements. |
Kotlin hashSetOf Function | Returns a HashSet containing the specified elements. |
Kotlin emptySet Function | Returns an empty immutable set. |
Kotlin setOfNotNull Function | Returns a set containing all elements that are not null. |
Kotlin mapOf Function | Returns an immutable map containing the specified key-value pairs. |
Kotlin mutableMapOf Function | Returns a mutable map containing the specified key-value pairs. |
Kotlin hashMapOf Function | Returns a HashMap containing the specified key-value pairs. |
Kotlin linkedMapOf Function | Returns a LinkedHashMap containing the specified key-value pairs. |
Kotlin emptyMap Function | Returns an empty immutable map. |
Kotlin mapOfNotNull Function | Returns a map containing all key-value pairs that are not null. |
Kotlin collectionSizeOrDefault Function | Returns the size of the collection if it’s not null or the default value otherwise. |
Kotlin asIterable Function | Returns an Iterable wrapping the original collection. |
Kotlin asSequence Function | Returns a Sequence wrapping the original collection. |
Kotlin filter Function | Returns a list containing only elements matching the given predicate. |
Kotlin filterNot Function | Returns a list containing only elements not matching the given predicate. |
Kotlin filterNotNull Function | Returns a list containing all elements that are not null. |
Kotlin filterTo Function | Appends all elements matching the given predicate to the given destination. |
Kotlin List mapNotNull Function | Returns a list containing only the non-null results of applying the given transform function to each element in the original list. |
Kotlin mapTo Function | Appends all elements transformed by the given function to the given destination. |
Kotlin List flatMap Function | Returns a single list of all elements yielded from results of applying the given transform function to each element of the original list. |
Kotlin List flatMapTo Function | Appends all elements yielded from results of applying the given transform function to each element of the original list to the given destination. |
Kotlin List groupBy Function | Groups elements of the original list by the key returned by the given selector function applied to each element and returns a map where each group key is associated with a list of corresponding elements. |
Kotlin List groupByTo Function | Groups elements of the original list by the key returned by the given selector function applied to each element and appends the results to the given destination map. |
Kotlin List partition Function | Splits the original list into a pair of lists, where the first list contains elements matching the given predicate and the second list contains elements not matching the predicate. |
Kotlin List reduce Function | Accumulates value starting with the first element and applying the given operation from left to right to current accumulator value and each element. |
The Kotlin List
class is part of the kotlin.collections
package and provides numerous utility functions to handle lists. These functions make it easy to manipulate lists, including operations such as querying, transforming, and filtering. Understanding and utilizing these functions can significantly simplify your Kotlin code and enhance its readability.
For more detailed information, please refer to the official Kotlin List Documentation.