Interview QuestionPractical QuestionFollow-up Questions

Kotlin Collection Extension Functions for Retrieval

skydovesJaewoong Eum (skydoves)||9 min read

Kotlin Collection Extension Functions for Retrieval

Kotlin provides several extension functions for extracting subsets or specific parts of collections. These functions enable efficient data retrieval while keeping the original collections unchanged. Understanding which function to use in each scenario avoids unnecessary copying and keeps data processing code readable. By the end of this lesson, you will be able to:

  • Use slice, take, takeLast, drop, and dropLast for positional extraction.
  • Apply chunked to break collections into fixed size groups.
  • Use windowed to create overlapping or sliding subsets.
  • Use zipWithNext for operations on consecutive element pairs.
  • Choose the right function based on whether you need positional, grouped, or sliding access.
  • Understand performance characteristics when chaining retrieval operations on large collections.

slice

The slice function extracts elements from a collection using a range or list of indices. It creates a new list containing the specified elements:

val items = listOf("skydoves", "kotlin", "developer", "android")
val sliced = items.slice(1..2)
println(sliced) // Output: [kotlin, developer]

You can also pass a list of specific indices to extract non contiguous elements: items.slice(listOf(0, 3)) returns [skydoves, android]. This makes slice more flexible than subList for non contiguous access patterns.

take and takeLast

take(n) returns the first n elements from the collection. takeLast(n) returns the last n elements:

val names = listOf("skydoves", "kotlin", "developer")
println(names.take(2))      // [skydoves, kotlin]
println(names.takeLast(2))  // [kotlin, developer]

If n exceeds the collection size, both functions return the entire collection without throwing an exception. This safe behavior avoids the need for manual bounds checking. There are also conditional variants: takeWhile and takeLastWhile accept a predicate and keep taking elements as long as the condition holds.

drop and dropLast

drop(n) skips the first n elements and returns the rest. dropLast(n) skips the last n elements:

val numbers = listOf(1, 2, 3, 4, 5)
println(numbers.drop(2))      // [3, 4, 5]
println(numbers.dropLast(2))  // [1, 2, 3]

Like take, there are conditional variants: dropWhile and dropLastWhile skip elements as long as the predicate is satisfied. These are useful for trimming leading or trailing elements that match a condition, such as removing whitespace entries from the beginning of a list.

chunked

The chunked(n) function breaks the collection into lists of size n. If the collection size is not divisible by n, the last chunk contains the remaining elements:

val data = listOf(1, 2, 3, 4, 5, 6)
val chunks = data.chunked(2)
println(chunks) // [[1, 2], [3, 4], [5, 6]]

An overloaded version accepts a transform function that processes each chunk immediately, avoiding the creation of intermediate lists: data.chunked(2) { it.sum() } returns [3, 7, 11]. This is useful for batch processing, such as uploading items to a server in groups or rendering grid rows.

This interview continues for subscribers

Subscribe to Dove Letter for full access to exclusive interviews about Android and Kotlin development.

Become a Sponsor