skip to content

Collection Operations

The extension-function toolbox over Iterable: transforming, aggregating, grouping, windowing, ordering, and searching. Interviewers often just hand you a data-shaping task and watch which operators you reach for.

part ofKotlinoverview, primer and where to startread it →
on this pageshow

explore

questions

page 2 of 2

Combine zip, unzip, windowed, and flatten to build a small pipeline: given two parallel lists of timestamps and temperatures, compute per-interval rate-of-change. Walk through your operator choices.

level: seniorimportance: nice to knowfreq 22%

basics

~20 s

Pair the two lists with zip so each reading is one (time, temp). Slide a 2-wide window over the readings to get consecutive pairs, then for each pair divide the temperature change by the time change to get the rate. unzip can split paired data back out when needed.

open as a page

When writing a custom Comparator for sortedWith, what correctness contract must it satisfy, and what goes wrong if it doesn't? How do compareBy/thenBy help you stay correct?

level: principalimportance: nice to knowfreq 25%

basics

~20 s

A comparator must be consistent: if a comes before b and b before c, then a must come before c, and reversing the arguments must reverse the sign. If it isn't, sorting can give wrong or unstable results, or even throw. Using compareBy/thenBy avoids hand-written mistakes.

open as a page

showing 31–32 of 32