Collections and Lambdas

Often in programming, we need to deal with collections of stuff. If you have worked with any major programming language, you will have dealt with collection types, such as:

Kotlin has these as well. Learning how to use them not only involves dealing with the actual types, but also with how to perform common operations on them, such as iterating over their contents.

Major Collection Types and Creation Functions

Kotlin has the aforementioned roster of collection types, and they form the most common collection types that you will use.

Kotlin/JVM also has access to other standard Java collection types, like LinkedHashMap, which we will discuss later in this chapter. However, usually you will use those only if there is no native Kotlin equivalent.

Arrays

Kotlin has an Array class that is analogous to Java arrays. If you use Kotlin arrays, and you are running on Kotlin/JVM, your Kotlin arrays will be mapped to Java arrays.

Overall, arrays are not quite as popular in Kotlin as are lists. However, you are welcome to use them, and they may be necessary if your code is going to interoperate with Java code.

To create an array, the simplest approach is to use the arrayOf() utility function:

  val things = arrayOf("foo", "bar", "goo")

  println(things::class)
  println(things)

Running this in the Klassbook gives us:

class Array
foo,bar,goo

Typed Arrays

Kotlin also has dedicated classes for arrays of primitive types:

They have corresponding utility functions to create instances, such as intArrayOf():

  val things = intArrayOf(1, 3, 3, 7)

  println(things::class)
  println(things)
class IntArray
1,3,3,7

As with Array, these type-specific arrays are not especially popular, except where you might need them for working with Java code that expects those primitive array types.

Lists

The basic one-dimensional collection type in Kotlin is the list. This is analogous to a Java ArrayList.

You can create instances of a list via listOf(), much like you create arrays with arrayOf() and the others mentioned above:

  val things = listOf("foo", "bar", "goo")

  println(things::class)
  println(things)

Technically, List is an interface. The concrete implementation that we see will be an ArrayList:

class ArrayList
[foo, bar, goo]

An unusual aspect of lists is that listOf() creates an “immutable” list, one where we cannot replace items in the list. We will explore immutability more later in this chapter.

Sets

Many programming languages offer a “set” collection type, which is a one-dimensional collection in which only distinct objects will be included. For example, if you try adding the four numbers 1, 3, 3, 7 to a list, you would have four entries, but adding them to a set results in a three-element set holding 1, 3, 7. The duplicate 3 value is ignored.

Kotlin has first-class support for sets, created using setOf():

  val things = setOf(1, 3, 3, 7)

  println(things::class)
  println(things)
  println(things.size)

As with List, Set is an interface. The concrete implementation that you will get from setOf() is a LinkedHashSet:

class LinkedHashSet
[1, 3, 7]
3

Among other stuff, this code snippet prints out the size of things. size is a property of a Set (and of List, for that matter), providing the number of elements in the collection. Here we see that the duplicate 3 values were replaced by a single 3 in the Set.

And, as with lists, setOf() creates an immutable Set, which we will cover later in this chapter.

Maps

Most programming languages have some support for a “map” or “dictionary” type, representing a key-value store. You can then place data into the collection as key-value pairs, to be able to eventually retrieve values given their keys. In Kotlin, we have a Map type that fills this role.

To create a hard-coded map — the way we have created hard-coded lists and sets — you can use the mapOf() utility function. This takes a comma-delimited list of key-value pairs, where the key and value are separated by the to keyword:

  val things = mapOf("key" to "value", "other-key" to "other-value")

  println(things::class)
  println(things)
  println(things.size)

Of course, you can combine these constructs, such as having a map of lists:

  val things = mapOf("odd" to listOf(1, 3, 5, 7, 9), "even" to listOf(2, 4, 6, 8))

  println(things::class)
  println(things)
  println(things.size)

(in truth, to is actually a function, leveraging some special syntax that we will see much later in the book)


Prev Table of Contents Next

This book is licensed under the Creative Commons Attribution-ShareAlike 4.0 International license.