liveBook · Manning

Chapter 6: Working with collections and sequences

What you will learn in chapter 6 of Kotlin in Action, 2E

Introduces functional collection processing as a common use of lambdas and previews sequences as an alternative for applying collection operations.

After this chapter you will be able to

What it builds on

Section by section

Introduction

Introduces functional collection processing as a common use of lambdas and previews sequences as an alternative for applying collection operations.

Key terms: lambdas, collections, standard library functions

6.1 Functional APIs for collections

Introduces why Kotlin’s lambda-customized standard-library functions are useful for working with collections.

Key terms: standard library, lambdas, functional programming style

6.1.1 Removing and transforming elements: filter and map

This section explains how to select and transform collection elements with filter and map, combine those operations, and use indexed and map-specific variants.

Key terms: filter, map, predicate, lambda, Person, maximum age, filterIndexed, mapIndexed, filterKeys, mapKeys

6.1.2 Accumulating values for collections: reduce and fold

Explains how reduce and fold accumulate collection elements into one value, and how their running counterparts retain intermediate results.

Key terms: reduce, fold, accumulator, lambda, runningReduce, runningFold, intermittent accumulator values

6.1.3 Applying a predicate to a collection: all, any, none, count, and find

This subsection explains how to test, count, and find collection elements with predicates, including the behavior of predicate checks on empty collections.

Key terms: predicate, all, any, none, empty collection, vacuous truth, count, filter, size, intermediate collection

6.1.4 Splitting a list into a pair of lists: partition

This section shows how partition splits a collection into two lists based on a Boolean predicate, more concisely than using filter and filterNot.

Key terms: partition, Boolean predicate, filter, filterNot, pair of lists

6.1.5 Converting a list to a map of groups: groupBy

This section shows how groupBy divides a collection into multiple groups and returns those groups as a map.

Key terms: groupBy, map, key, Map<Int, List<Person>>, first, extension, member reference

6.1.6 Transforming collections into maps: associate, associateWith, and associateBy

This section explains how to turn a collection into a map with associate, associateWith, or associateBy, and what happens when generated keys repeat.

Key terms: associate, key-value pair, to, associateWith, associateBy, keys, overwrites

6.1.7 Replacing elements in mutable collections: replaceAll and fill

This section explains two Kotlin functions for changing the elements of a mutable list: replaceAll and fill.

Key terms: MutableList, replaceAll, fill, lambda

6.1.8 Handling special cases for collections: ifEmpty

Introduces defaults for empty collections and for strings that are empty or contain only whitespace.

Key terms: ifEmpty, lambda, default value, ifBlank, whitespace

6.1.9 Splitting collections: chunked and windowed

This section shows how to process consecutive collection elements with overlapping windows or nonoverlapping chunks.

Key terms: windowed, sliding window, chunked, chunk

6.1.10 Merging collections: zip

Explains how zip combines corresponding elements of collections, including its output size, transformation option, and behavior when chained.

Key terms: zip, lambda, pairs, infix function, nested pairs

6.1.11 Processing elements in nested collections: flatMap and flatten

This section shows how to turn nested collections into a flat result with flatMap or flatten and encourages looking for standard library transformations.

Key terms: map, flatMap, toSet, nested collection, flatten, general transformation, Kotlin standard library, IDE’s autocomplete suggestions

6.2 Lazy collection operations: Sequences

This section contrasts eager collection chains with lazy sequences and explains when to convert between sequences and lists.

Key terms: eagerly, intermediate collections, Sequence, lazily, map, filter, iterator, asSequence, toList

6.2.1 Executing sequence operations: Intermediate and terminal operations

This section explains when sequence operations run, how they process elements, and why operation order can affect performance.

Key terms: intermediate operation, terminal operation, sequence, toList, lazy evaluation, eager evaluation, map, find, filter, chain of operations

6.2.2 Creating sequences

This section shows how to create sequences from a starting element and a rule for producing the next one, then use them to process numbers or traverse parent directories lazily.

Key terms: generateSequence, takeWhile, terminal operation, postponed computation, parent directories, any, find

6.3 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 5: Programming with lambdas Chapter 7: Working with nullable values →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.