Skip to content

Instantly share code, notes, and snippets.

@magdamiu

magdamiu/Collections.kt

Last active Mar 14, 2020
Embed
What would you like to do?
Immutable and mutable lists
// immutable list and mutable list
val numbersList = listOf("one", "two", "three")
val mutableNumbersList = mutableListOf("one", "two", "three")
// alter the list using add function
mutableNumbersList.add("five")
listOf(1, 5, 3).sum() // => 9
listOf("a", "b", "cc").sumBy { it.length } // => 4
// operators
val numberList2 = numbersList + "four"
println(numberList2)
val numbersListNoTwo = numberList2 - "two"
println(numbersListNoTwo)
println(numbersListNoTwo::class) //class java.util.ArrayList
println(numbersListNoTwo.javaClass) //class java.util.ArrayList
// listOfNotNull
val notNulls = listOfNotNull(32, null, "one", null, 'd')
println("Size = ${notNulls.size}")
for (element in notNulls) {
println(element)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment