Chapter 2. The Kotlin Collections Framework

In the preceding chapter we offered an overview of the syntax of the Kotlin language. As with any language, syntax is a foundation but, really, no more than that. When it comes to getting actual work done, syntax alone won’t carry the water. To do that you need expressions and idioms that are easy to assemble into useful code, and that are as easy for other developers to understand and modify.

One important aspect of nearly every modern language is its collections framework: ways of grouping objects, and libraries of functions that manipulate them.

At the time it was introduced, Java’s collection framework was state of the art. Today, more than 20 years later, the basic data structures provided by newer languages have not changed much. All of the containers that we’re familiar with from the Java framework (or even the earliest versions of the C++ stdlib) are still there: Iterable, Collection, List, Set, and Map (to use their Java names). In response to broad acceptance of functional styles of programming, however, collections frameworks for modern languages like Swift and Scala usually provide a set of common, higher-order functions that operate on the collections: filter, map, flatmap, zip, and more. You will, indeed, find these functions in the collections framework from the Kotlin Standard Library.

In this chapter, we will first visit the collections themselves and a few interesting extensions that the Kotlin language empowers. After ...

Get Programming Android with Kotlin now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.