Skip to content

Instantly share code, notes, and snippets.

@magdamiu
Last active March 14, 2020 13:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save magdamiu/cd055fab88ff45d5787ba7425f67032d to your computer and use it in GitHub Desktop.
Save magdamiu/cd055fab88ff45d5787ba7425f67032d to your computer and use it in GitHub Desktop.
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