Junior
What standard functions for working with collections in Kotlin do you know?
sobes.tech AI
Answer from AI
Standard functions for working with collections in Kotlin are very diverse and allow performing many operations:
Transformation operations:
map: Transforms each element of the collection into a new element, returning a new collection with the results.flatMap: Transforms each element into a collection of elements and combines all resulting collections into one.zip: Creates pairs from elements of two collections with the same indices.unzip: Splits a collection of pairs into two collections.
Filtering operations:
filter: Returns a collection containing only elements that satisfy the given predicate.filterNotNull: Returns a new collection containing only non-null elements.filterIsInstance: Returns a collection containing only elements of the specified type.distinct: Returns a collection with unique elements.drop: Returns a collection without the first N elements.dropLast: Returns a collection without the last N elements.take: Returns the first N elements of the collection.takeLast: Returns the last N elements of the collection.
Ordering operations:
sorted: Returns a sorted collection in natural order.sortedBy: Returns a collection sorted by the result of a specified selector for each element.sortedDescending: Returns a collection sorted in reverse natural order.sortedByDescending: Returns a collection sorted in reverse order by the result of a specified selector.reversed: Returns a list with elements in reverse order.
Aggregation (reduction) operations:
fold: Reduces the collection to a single value by applying a function to an accumulator and each element, with an initial accumulator value.reduce: Reduces the collection to a single value by applying a function to an accumulator and each element, starting with the first element as the initial accumulator.sum: Calculates the sum of all numeric elements in the collection.average: Calculates the average of numeric elements in the collection.count: Returns the number of elements in the collection.maxOrNull: Returns the maximum element or null if the collection is empty.minOrNull: Returns the minimum element or null if the collection is empty.maxByOrNull: Returns the element for which the specified selector returns the maximum value.minByOrNull: Returns the element for which the specified selector returns the minimum value.
Element search operations:
find: Returns the first element satisfying the predicate or null.findLast: Returns the last element satisfying the predicate or null.first: Returns the first element.last: Returns the last element.firstOrNull: Returns the first element or null if the collection is empty.lastOrNull: Returns the last element or null if the collection is empty.indexOf: Returns the index of the first occurrence of the specified element.lastIndexOf: Returns the index of the last occurrence of the specified element.single: Returns the single element if present; throws an exception if there are no elements or more than one.singleOrNull: Returns the single element or null if there are no elements or more than one.
Operations on parts of the collection:
slice: Returns a list of elements at specified indices.chunked: Divides the collection into lists of a specified size.windowed: Creates sliding windows of a specified size.
Grouping operations:
groupBy: Groups elements by a key, returning a Map where the key is the result of the selector function, and the value is a list of elements with that key.groupingBy: Returns a Grouping object that allows lazy aggregations over groups.
Check operations:
all: Checks if all elements satisfy the predicate.any: Checks if any element satisfies the predicate.none: Checks if no elements satisfy the predicate.contains: Checks if the collection contains the specified element.isEmpty: Checks if the collection is empty.isNotEmpty: Checks if the collection is not empty.
This is just a part of the standard functions available for working with collections in Kotlin. Many of them have overloads for different collection types and parameters.
Example of using map:
// List of numbers
val numbers = listOf(1, 2, 3, 4, 5)
// Transform each number into its square
val squares = numbers.map { it * it }
// squares: [1, 4, 9, 16, 25]
Example of using filter:
// List of strings
val fruits = listOf("apple", "banana", "cherry", "date")
// Filter by string length greater than 5
val longFruits = fruits.filter { it.length > 5 }
// longFruits: [banana, cherry]