Collection Operations: Overview
A list of the collection transformations you should definitely know about, and an overview of patterns in the names of operations that you will encounter.
— — — — — — — — — — — — — — —
THE CURRENT VERSION OF THIS ARTICLE IS PUBLISHED HERE.
— — — — — — — — — — — — — — —

Tags: #FYI++
This article is part of the Kotlin Primer, an opinionated guide to the Kotlin language, which is indented to help facilitate Kotlin adoption inside Java-centric organizations. It was originally written as an organizational learning resource for Etnetera a.s. and I would like to express my sincere gratitude for their support.
It is recommended to read the Introduction before moving on. Check out the Table of Contents for all articles.
In the previous article, we talked about the usefulness of both the object-oriented and functional styles, and explained when you should use which:
Structure your programs using OOP, and implement behaviors using FP.
Of course, to be able to do implement behaviors using FP, you have to learn the names and purposes of the FP building blocks, in the same way you learned OOP patterns — builders, factories etc. The Kotlin Standard Library is chock-full with them, and it is the purpose of the following set of articles to give you a passing familiarity with those that are used most often.
However, unless you’re already familiar with this style of thinking, it will probably be more that you can memorize, so instead, I recommend this: focus on designing behaviors in the same way you design programs. Think before you write, decompose your behaviors into atomic pieces, and then search the internet to see if the standard library already contains something similar. Over time, you’ll absorb the contents of the standard library and learn to get a feeling for which patterns are “core” and almost certainly implemented vs. which ones aren’t, and need to be implemented locally.
Collection operations
To give you an idea of the breadth covered by the standard library, here is a (partial) list of available functions. We will be covering a large part of them in the following articles. All of them are defined as extension functions.
See if you can figure out what they do just based on the name.
Transformers
associate, flatten, flatMap, intersect, map, mapNotNull, mapIndexed, mapIndexedNotNull, reversed, sorted, sortedByDescending, sortedWith, union, unzip, zip
Filtering
binarySearch, distinct, distinctBy, drop, dropWhile, dropLast, dropLastWhile, filter, filterIndexed, filterIsInstance, orEmpty, slice, take, takeWhile, takeLast, takeLastWhile
Single element access
elementAt, elementAtOrElse, elementAtOrNull, find, findLast, first, firstOrNull, get, getOrElse, indexOf, indexOfFirst, indexOfLast, last, lastIndexOf, lastOrNull, single, singleOrNull
Predicates
all, any, contains, containsAll, none, isEmpty, isNotEmpty
Aggregators
fold, foldIndexed, foldRight, foldRightIndexed, reduce, reduceIndexed, reduceRight, reduceRightIndexed, average, count, max, maxBy, maxOf, maxWith, min, minBy,minOf, minWith, sum, sumOf
Grouping
groupBy, groupByTo, groupingBy, partition
Naming patterns
There are a few patterns in the way collection operations are named that are useful to be aware of. For a operation {o} (e.g. map), usually at least some of the following are defined:
{o}To (e.g. mapTo)
Always accepts a mutable collection as one of its parameters. Instead of returning a new collection, as the result of the transformation, instead inserts the result into the mutable collection that was passed.

