# Collections When programming, it is useful to be able to group data into structures for later processing. Kotlin provides collections for exactly this purpose. Kotlin has the following collections for grouping items: | Collection type |Description | -------------------------------- | Lists |Ordered collections of items | | Sets |Unique unordered collections of items | | Maps |Sets of key-value pairs where keys are unique and map to only one value | Each collection type can be mutable or read only. ## List Lists store items in the order that they are added, and allow for duplicate items. To create a read-only list ([List](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-list/)), use the [listOf()](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/list-of.html) function. To create a mutable list ([MutableList](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-mutable-list.html)), use the [mutableListOf()](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/mutable-list-of.html) function. When creating lists, Kotlin can infer the type of items stored. To declare the type explicitly, add the type within angled brackets `<>` after the list declaration: ```KOTLIN fun main() { //sampleStart // Read only list val readOnlyShapes = listOf("triangle", "square", "circle") println(readOnlyShapes) // [triangle, square, circle] // Mutable list with explicit type declaration val shapes: MutableList = mutableListOf("triangle", "square", "circle") println(shapes) // [triangle, square, circle] //sampleEnd } ``` Tip: To prevent unwanted modifications, you can create a read-only view of a mutable list by assigning it to a `List`: ```KOTLIN val shapes: MutableList = mutableListOf("triangle", "square", "circle") val shapesLocked: List = shapes ``` This is also called casting. Lists are ordered so to access an item in a list, use the [indexed access operator](operator-overloading.html#indexed-access-operator) `[]`: ```KOTLIN fun main() { //sampleStart val readOnlyShapes = listOf("triangle", "square", "circle") println("The first item in the list is: ${readOnlyShapes[0]}") // The first item in the list is: triangle //sampleEnd } ``` To get the first or last item in a list, use [.first()](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/first.html) and [.last()](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/last.html) functions respectively: ```KOTLIN fun main() { //sampleStart val readOnlyShapes = listOf("triangle", "square", "circle") println("The first item in the list is: ${readOnlyShapes.first()}") // The first item in the list is: triangle //sampleEnd } ``` Note: [.first()](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/first.html) and [.last()](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/last.html) functions are examples of extension functions. To call an extension function on an object, write the function name after the object appended with a period `.` Extension functions are covered in detail [in the intermediate tour](kotlin-tour-intermediate-extension-functions.html#extension-functions). For now, you only need to know how to call them. To get the number of items in a list, use the [.count()](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/count.html) function: ```KOTLIN fun main() { //sampleStart val readOnlyShapes = listOf("triangle", "square", "circle") println("This list has ${readOnlyShapes.count()} items") // This list has 3 items //sampleEnd } ``` To check that an item is in a list, use the [in operator](operator-overloading.html#in-operator): ```KOTLIN fun main() { //sampleStart val readOnlyShapes = listOf("triangle", "square", "circle") println("circle" in readOnlyShapes) // true //sampleEnd } ``` To add or remove items from a mutable list, use [.add()](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-mutable-list/add.html) and [.remove()](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/remove.html) functions respectively: ```KOTLIN fun main() { //sampleStart val shapes: MutableList = mutableListOf("triangle", "square", "circle") // Add "pentagon" to the list shapes.add("pentagon") println(shapes) // [triangle, square, circle, pentagon] // Remove the first "pentagon" from the list shapes.remove("pentagon") println(shapes) // [triangle, square, circle] //sampleEnd } ``` ## Set Whereas lists are ordered and allow duplicate items, sets are unordered and only store unique items. To create a read-only set ([Set](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-set/)), use the [setOf()](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/set-of.html) function. To create a mutable set ([MutableSet](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-mutable-set/)), use the [mutableSetOf()](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/mutable-set-of.html) function. When creating sets, Kotlin can infer the type of items stored. To declare the type explicitly, add the type within angled brackets `<>` after the set declaration: ```KOTLIN fun main() { //sampleStart // Read-only set val readOnlyFruit = setOf("apple", "banana", "cherry", "cherry") // Mutable set with explicit type declaration val fruit: MutableSet = mutableSetOf("apple", "banana", "cherry", "cherry") println(readOnlyFruit) // [apple, banana, cherry] //sampleEnd } ``` You can see in the previous example that because sets only contain unique elements, the duplicate `"cherry"` item is dropped. Tip: To prevent unwanted modifications, you can create a read-only view of a mutable set by assigning it to a `Set`: ```KOTLIN val fruit: MutableSet = mutableSetOf("apple", "banana", "cherry", "cherry") val fruitLocked: Set = fruit ``` Note: As sets are unordered, you can't access an item at a particular index. To get the number of items in a set, use the [.count()](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/count.html) function: ```KOTLIN fun main() { //sampleStart val readOnlyFruit = setOf("apple", "banana", "cherry", "cherry") println("This set has ${readOnlyFruit.count()} items") // This set has 3 items //sampleEnd } ``` To check that an item is in a set, use the [in operator](operator-overloading.html#in-operator): ```KOTLIN fun main() { //sampleStart val readOnlyFruit = setOf("apple", "banana", "cherry", "cherry") println("banana" in readOnlyFruit) // true //sampleEnd } ``` To add or remove items from a mutable set, use [.add()](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-mutable-set/add.html) and [.remove()](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/remove.html) functions respectively: ```KOTLIN fun main() { //sampleStart val fruit: MutableSet = mutableSetOf("apple", "banana", "cherry", "cherry") fruit.add("dragonfruit") // Add "dragonfruit" to the set println(fruit) // [apple, banana, cherry, dragonfruit] fruit.remove("dragonfruit") // Remove "dragonfruit" from the set println(fruit) // [apple, banana, cherry] //sampleEnd } ``` ## Map Maps store items as key-value pairs. You access the value by referencing the key. You can imagine a map like a food menu. You can find the price (value), by finding the food (key) you want to eat. Maps are useful if you want to look up a value without using a numbered index, like in a list. Note: * Every key in a map must be unique so that Kotlin can understand which value you want to get. * You can have duplicate values in a map. To create a read-only map ([Map](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-map/)), use the [mapOf()](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/map-of.html) function. To create a mutable map ([MutableMap](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-mutable-map/)), use the [mutableMapOf()](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/mutable-map-of.html) function. When creating maps, Kotlin can infer the type of items stored. To declare the type explicitly, add the types of the keys and values within angled brackets `<>` after the map declaration. For example: `MutableMap`. The keys have type `String` and the values have type `Int`. The easiest way to create maps is to use [to](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/to.html) between each key and its related value: ```KOTLIN fun main() { //sampleStart // Read-only map val readOnlyJuiceMenu = mapOf("apple" to 100, "kiwi" to 190, "orange" to 100) println(readOnlyJuiceMenu) // {apple=100, kiwi=190, orange=100} // Mutable map with explicit type declaration val juiceMenu: MutableMap = mutableMapOf("apple" to 100, "kiwi" to 190, "orange" to 100) println(juiceMenu) // {apple=100, kiwi=190, orange=100} //sampleEnd } ``` Tip: To prevent unwanted modifications, you can create a read-only view of a mutable map by assigning it to a `Map`: ```KOTLIN val juiceMenu: MutableMap = mutableMapOf("apple" to 100, "kiwi" to 190, "orange" to 100) val juiceMenuLocked: Map = juiceMenu ``` To access a value in a map, use the [indexed access operator](operator-overloading.html#indexed-access-operator) `[]` with its key: ```KOTLIN fun main() { //sampleStart // Read-only map val readOnlyJuiceMenu = mapOf("apple" to 100, "kiwi" to 190, "orange" to 100) println("The value of apple juice is: ${readOnlyJuiceMenu["apple"]}") // The value of apple juice is: 100 //sampleEnd } ``` Note: If you try to access a key-value pair with a key that doesn't exist in a map, you see a `null` value: ```KOTLIN fun main() { //sampleStart // Read-only map val readOnlyJuiceMenu = mapOf("apple" to 100, "kiwi" to 190, "orange" to 100) println("The value of pineapple juice is: ${readOnlyJuiceMenu["pineapple"]}") // The value of pineapple juice is: null //sampleEnd } ``` This tour explains null values later in the [Null safety](kotlin-tour-null-safety.html) chapter. You can also use the [indexed access operator](operator-overloading.html#indexed-access-operator) `[]` to add items to a mutable map: ```KOTLIN fun main() { //sampleStart val juiceMenu: MutableMap = mutableMapOf("apple" to 100, "kiwi" to 190, "orange" to 100) juiceMenu["coconut"] = 150 // Add key "coconut" with value 150 to the map println(juiceMenu) // {apple=100, kiwi=190, orange=100, coconut=150} //sampleEnd } ``` To remove items from a mutable map, use the [.remove()](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/remove.html) function: ```KOTLIN fun main() { //sampleStart val juiceMenu: MutableMap = mutableMapOf("apple" to 100, "kiwi" to 190, "orange" to 100) juiceMenu.remove("orange") // Remove key "orange" from the map println(juiceMenu) // {apple=100, kiwi=190} //sampleEnd } ``` To get the number of items in a map, use the [.count()](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/count.html) function: ```KOTLIN fun main() { //sampleStart // Read-only map val readOnlyJuiceMenu = mapOf("apple" to 100, "kiwi" to 190, "orange" to 100) println("This map has ${readOnlyJuiceMenu.count()} key-value pairs") // This map has 3 key-value pairs //sampleEnd } ``` To check if a specific key is already included in a map, use the [.containsKey()](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/contains-key.html) function: ```KOTLIN fun main() { //sampleStart val readOnlyJuiceMenu = mapOf("apple" to 100, "kiwi" to 190, "orange" to 100) println(readOnlyJuiceMenu.containsKey("kiwi")) // true //sampleEnd } ``` To obtain a collection of the keys or values of a map, use the [keys](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-map/keys.html) and [values](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-map/values.html) properties respectively: ```KOTLIN fun main() { //sampleStart val readOnlyJuiceMenu = mapOf("apple" to 100, "kiwi" to 190, "orange" to 100) println(readOnlyJuiceMenu.keys) // [apple, kiwi, orange] println(readOnlyJuiceMenu.values) // [100, 190, 100] //sampleEnd } ``` Note: [keys](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-map/keys.html) and [values](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-map/values.html) are examples of properties of an object. To access the property of an object, write the property name after the object appended with a period `.` Properties are discussed in more detail in the [Classes](kotlin-tour-classes.html) chapter. At this point in the tour, you only need to know how to access them. To check that a key or value is in a map, use the [in operator](operator-overloading.html#in-operator): ```KOTLIN fun main() { //sampleStart val readOnlyJuiceMenu = mapOf("apple" to 100, "kiwi" to 190, "orange" to 100) println("orange" in readOnlyJuiceMenu.keys) // true // Alternatively, you don't need to use the keys property println("orange" in readOnlyJuiceMenu) // true println(200 in readOnlyJuiceMenu.values) // false //sampleEnd } ``` For more information on what you can do with collections, see [Collections](collections-overview.html). Now that you know about basic types and how to manage collections, it's time to explore the [control flow](kotlin-tour-control-flow.html) that you can use in your programs. ## Practice Count the total number of items in two lists : You have a list of “green” numbers and a list of “red” numbers. Complete the code to print how many numbers there are in total. : ```KOTLIN // Keep these two lists as they are. Use them in main(). val greenNumbers = listOf(1, 4, 23) val redNumbers = listOf(17, 2) fun main() { // Write your code here } ``` : ```KOTLIN val greenNumbers = listOf(1, 4, 23) val redNumbers = listOf(17, 2) fun main() { val totalCount = greenNumbers.count() + redNumbers.count() println(totalCount) } ``` Check whether a requested protocol is supported : You have a set of protocols supported by your server. A user requests to use a particular protocol. Complete the program to check whether the requested protocol is supported or not (`isSupported` must be a Boolean value). : ```KOTLIN fun main() { val SUPPORTED = setOf("HTTP", "HTTPS", "FTP") val requested = "smtp" val isSupported = TODO() // Replace the TODO() function println("Support for $requested: $isSupported") } ``` : Hint : Make sure that you check the requested protocol in upper case. You can use the : [.uppercase()](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/uppercase.html) : function to help you with this. : ```KOTLIN fun main() { val SUPPORTED = setOf("HTTP", "HTTPS", "FTP") val requested = "smtp" val isSupported = requested.uppercase() in SUPPORTED println("Support for $requested: $isSupported") } ``` Spell out a number using a map : Define a map that relates integer numbers from 1 to 3 to their corresponding spelling. Use this map to spell the given number. : ```KOTLIN // Replace the empty map. Keep the variable name as number2word. val number2word: Map = emptyMap() fun main() { val n = 2 println("$n is spelled as '${number2word[n]}'") } ``` : ```KOTLIN val number2word = mapOf(1 to "one", 2 to "two", 3 to "three") fun main() { val n = 2 println("$n is spelled as '${number2word[n]}'") } ``` ## See also * [Previous step](kotlin-tour-basic-types.html) * [Next step](kotlin-tour-control-flow.html)