this repo has no description
at main 5.0 kB view raw
1* Collections 2 3Kotlin has the following collections for grouping items: 4 5| Collection type | Description | 6|-----------------+-------------------------------------------------------------------------| 7| Lists | Ordered collections of items | 8| Sets | Unique unordered collections of items | 9| Maps | Sets of key-value pairs where keys are unique and map to only one value | 10 11** List 12 13To create a read-only list use the =listOf()= function. 14 15#+begin_src kotlin 16 val readOnlyShapes = listOf("triangle", "square", "circle") 17 println(readOnlyShapes) 18#+end_src 19 20#+RESULTS: 21: [triangle, square, circle] 22 23To create a mutable list use the =mutableListOf()= function. 24 25#+begin_src kotlin 26 val shapes: MutableList<String> = mutableListOf("triangle", "square", "circle") 27 println(shapes) 28#+end_src 29 30#+RESULTS: 31: [triangle, square, circle] 32 33You can obtain read-only views of mutable lists by assigning them to a =List=: 34 35#+begin_src kotlin 36 val shapes2: MutableList<String> = mutableListOf("triangle") 37 val shapes2Locked: List<String> = shapes2 38 println(shapes2Locked) 39#+end_src 40 41#+RESULTS: 42: [triangle] 43 44Lists are ordered so to access an item in a list use the indexed access operator =[]= 45 46#+begin_src kotlin 47 println("The first item in the list is: ${readOnlyShapes[0]}") 48#+end_src 49 50#+RESULTS: 51: The first item in the list is: triangle 52 53Or you can get the first using the =.first()= extension: 54 55#+begin_src kotlin 56 println("The first item in the list is: ${readOnlyShapes.first()}") 57#+end_src 58 59#+RESULTS: 60: The first item in the list is: triangle 61 62There's also the =.last()= extension to get the last item of a List: 63 64#+begin_src kotlin 65 println("The last item in the list is: ${readOnlyShapes.last()}") 66#+end_src 67 68#+RESULTS: 69: The last item in the list is: circle 70 71To get the number of items in a list, use =.count()= extension 72 73#+begin_src kotlin 74 println("The list has ${readOnlyShapes.count()} items") 75#+end_src 76 77#+RESULTS: 78: The list has 3 items 79 80To check that an item is in a list, use =in= operator 81 82#+begin_src kotlin 83 println("circle" in readOnlyShapes) 84#+end_src 85 86#+RESULTS: 87: true 88 89To add or remove items from a mutable list, use =.add()= and =.remove()= functions 90 91#+begin_src kotlin 92 shapes.add("pentagon") 93 println(shapes) 94 95 shapes.remove("pentagon") 96 println(shapes) 97#+end_src 98 99#+RESULTS: 100: [triangle, square, circle, pentagon] 101: [triangle, square, circle] 102 103** Set 104 105Whereas lists are ordered and allow duplicate items, sets are *unordered* and only store *unique* items. 106 107To create a read-only set, use the =setOf()= function. 108 109#+begin_src kotlin 110 val readOnlyFruit = setOf("apple", "banana", "cherry") 111#+end_src 112 113#+RESULTS: 114 115To create a mutable set, use the =mutableSetOf()= function. 116 117#+begin_src kotlin 118 val fruit: MutableSet<String> = mutableSetOf("apple", "banana", "cherry") 119#+end_src 120 121#+RESULTS: 122 123As sets are *unordered*, you can't access an item at a particular index. 124 125To get the number of items, use the =.count()= function 126 127#+begin_src kotlin 128 println("This set has ${readOnlyFruit.count()} items") 129#+end_src 130 131#+RESULTS: 132: This set has 3 items 133 134To check that an item is in a set, use the =in= operator 135 136#+begin_src kotlin 137 println("banana" in readOnlyFruit) 138#+end_src 139 140#+RESULTS: 141: true 142 143To add or remove items from a mutable set, use =.add()= and =.remove()= functions 144 145#+begin_src kotlin 146 fruit.add("dragonfruit") 147 println(fruit) 148 149 fruit.remove("dragonfruit") 150 println(fruit) 151#+end_src 152 153#+RESULTS: 154: [apple, banana, cherry, dragonfruit] 155: [apple, banana, cherry] 156 157** Map 158 159Maps store items as key-value pairs. 160 161To creat a read-only map, use the =mapOf()= function 162 163#+begin_src kotlin 164 val readOnlyMenu = mapOf( 165 "apple" to 100, 166 "kiwi" to 100, 167 "orange" to 200 168 ) 169#+end_src 170 171#+RESULTS: 172 173To creat a mutable map, use the =mutableMapOf()= function 174 175#+begin_src kotlin 176 val menu: MutableMap<String, Int> = mutableMapOf( 177 "apple" to 100, 178 "kiwi" to 100, 179 "orange" to 200 180 ) 181#+end_src 182 183#+RESULTS: 184 185To access a value in a map, use the indexed access operator =[]= with its key 186 187#+begin_src kotlin 188 println("The value of apple juice is: ${readOnlyMenu["apple"]}") 189#+end_src 190 191#+RESULTS: 192: The value of apple juice is: 100 193 194To get the number of items in a map, use the =.count()= function 195 196#+begin_src kotlin 197 println("This map has ${readOnlyMenu.count()} key-value pairs") 198#+end_src 199 200#+RESULTS: 201: This map has 3 key-value pairs 202 203To add or remove items from a mutable map, use =.put()= and =.remove()= functions 204 205#+begin_src kotlin 206 menu.put("coconut", 150) 207 println(menu) 208 209 menu.remove("orange") 210 println(menu) 211#+end_src 212 213#+RESULTS: 214: {apple=100, kiwi=100, orange=200, coconut=150} 215: {apple=100, kiwi=100, coconut=150} 216 217To check if a specific key is already included in a map, use the =.containsKey()= function 218 219#+begin_src kotlin 220 println(readOnlyMenu.containsKey("kiwi")) 221#+end_src 222 223#+RESULTS: 224: true