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
// you need to first convert to map | |
mutableListOf<Pair<String,Int>>(pair,updatedPair).toMap() | |
// then you have access to mutableMap extension | |
mutableListOf<Pair<String,Int>>(pair,updatedPair).toMap().toMutableMap() | |
//signature | |
fun <K, V> Map<out K, V>.toMutableMap(): MutableMap<K, V> |
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
val pair = "2" to 1 | |
val updatedPair = pair.copy(first = "1") |
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
// return old value, if updating existing entry else null | |
abstract fun put(key: K, value: V): V? | |
abstract fun putAll(from: Map<out K, V>) | |
abstract fun clear() | |
// return removed value, if not existing then null | |
abstract fun remove(key: K): V? | |
open fun remove(key: K, value: V): Boolean |
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
abstract val entries: MutableSet<MutableEntry<K, V>> is a MutableSet |
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
abstract fun setValue(newValue: V): V |
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
interface MutableMap<K, V> : Map<K, V> |
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
fun <K, V> mapOf(vararg pairs: Pair<K, V>): Map<K, V> | |
= if (pairs.size > 0){ | |
pairs.toMap(LinkedHashMap(mapCapacity(pairs.size))) | |
}else{ | |
emptyMap() | |
} | |
// Linked HashMap signature | |
class LinkedHashMap<K, V> : MutableMap<K, V> | |
// since MutableMap<K, V> has Map as parent class | |
// Casting it to map make it immutable* (read-only) |
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
fun <K, V> mapOf(pair: Pair<K, V>): Map<K, V> | |
= java.util.Collections.singletonMap(pair.first, pair.second) | |
// In java.util.Collections, | |
public static Map singletonMap(K key, V value) | |
//a function that return an immutable map, which is serializable. | |
//it is mapping only the specified key to the specified value. |
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
fun <K, V> mapOf(): Map<K, V> = emptyMap() | |
// emptyMap is resolved by | |
object EmptyMap : Map<Any?, Nothing>, Serializable | |
// which return read-only empty map implementation |
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
// Kotlin Standard Library function | |
fun <K, V> mapOf(): Map<K, V> | |
fun <K, V> mapOf(pair: Pair<K, V>): Map<K, V> | |
fun <K, V> mapOf(vararg pairs: Pair<K, V>): Map<K, V> |