🐦 Kotlin Intermediate

What is the chunked function in Kotlin?

Answer

chunked(size) splits a collection into a list of sublists, each with at most the specified number of elements. The last chunk may be smaller if the collection size is not evenly divisible. Example: listOf(1, 2, 3, 4, 5).chunked(2) gives [[1, 2], [3, 4], [5]]. This is useful for batch processing API requests or splitting a large dataset into manageable pages. For a sliding window over the collection, use windowed(size, step) instead.