September 2019
Intermediate to advanced
462 pages
11h 3m
English
Tuples are sequences of objects of small, finite size. Unlike some languages that provide a way to create tuples of different sizes, Kotlin provides two specific types: Pair for a tuple of size two and Triple for a size of three. Use these two when you want to quickly create two or three objects as a collection.
Here’s an example of creating a Pair of Strings:
| | println(Pair("Tom", "Jerry")) //(Tom, Jerry) |
| | println(mapOf("Tom" to "Cat", "Jerry" to "Mouse")) //{Tom=Cat, Jerry=Mouse} |
First we create an instance of Pair using the constructor. Then we use the to() extension function, that’s available on any object in Kotlin, to create pairs of entries for a Map. The to() method creates an instance of Pair, with ...
Read now
Unlock full access