Created
November 29, 2018 00:18
-
-
Save cassiozen/d9417d1078a420c8a6555623b55896c0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// mapOf is the read-only version of mutableMapof | |
val colors = mapOf( | |
"red" to 0xff0000, | |
"green" to 0x00ff00, | |
"blue" to 0x0000ff | |
) | |
val updatedColors = colors.plus("teal" to 0x008080) // doesn't change the original - it returns a new map | |
// listOf is the read-only version of mutableListof | |
val houses = listOf("Stark", "Lannister", "Tyrell", "Arryn", "Targaryen", "Martell", "Baratheon") | |
// Methods that return a new list instead of modifying it are still available: | |
var updatedHouses = houses.take(3).map {it.toUpperCase()} //["STARK", "LANNISTER", "TYRELL"] | |
// Adding new items requires copying the whole original one and making sure the new copy is also immutable | |
var updatedHouses = houses.toMutableList().apply{ add("Martell") }.toList() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment