Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Collections in Kotlin

Collections in Kotlin

5-minute talk about collections in Kotlin

Harri Kirik

April 02, 2018
Tweet

More Decks by Harri Kirik

Other Decks in Programming

Transcript

  1. KOTLIN: COLLECTIONS val numbers: MutableList<Int> = mutableListOf(1, 2, 3) val

    readOnlyView: List<Int> = numbers println(numbers) // prints "[1, 2, 3]" numbers.add(4) println(readOnlyView) // prints "[1, 2, 3, 4]" readOnlyView.clear() // -> does not compile val strings = hashSetOf("a", "b", "c", "c") assert(strings.size == 3) #demoday
  2. KOTLIN: COLLECTIONS val mutable = mutableListOf(1, 2, 3) val snapshot

    = mutable.toList() mutable.add(4) println(mutable) // prints "[1, 2, 3, 4]" println(snapshot) // prints "[1, 2, 3]" #demoday
  3. KOTLIN: COLLECTIONS val mutable = mutableListOf(1, 2, 3) val snapshot

    = mutable.toList() snapshot.add(4) // Will not compile! val snapshot2 = snapshot.plus(4) // Returns a new list println(snapshot2 ) // prints "[1, 2, 3, 4]" #demoday