Sudhir Singh Khanger
10/15/2019, 5:31 AMMap/MutableMap
which is a LinkedHashMap
which preserves the insertion order unlike HashMap
. In Java/Android, if one is used to using HashMap
then I would think I can continue using Map/MutableMap
in Kotlin. Is that correct?diesieben07
10/15/2019, 7:03 AMLinkedHashMap
and HashMap
implement MutableMap
, which extends Map
.Sudhir Singh Khanger
10/15/2019, 8:48 AMLinkedHashMap
. Although I don't think I need to maintain order but I might as well use it.diesieben07
10/15/2019, 8:56 AMkarelpeeters
10/15/2019, 8:58 AMLinkedHashSet
is the default, now everybody is paying this cost all over the code and you need to actively think about using hashSetOf
instead of setOf
.eekboom
10/15/2019, 9:25 AMSudhir Singh Khanger
10/16/2019, 4:19 PMlinkedMapOf(1 to "z")
and mapOf(1 to "z")
?
Or is LinkedHashMap
a Java class whereas Map
is Kotlin class, if there is such a thing, and by extension I should use Kotlin specific classes where ever possible.karelpeeters
10/16/2019, 4:23 PMlinkedMapOf
always returns a LinkedHashMap
, mapOf
returns different types for an empty map. Also ´linkedMapOf` is mutable.linkedMapOf
to emphasize that the order matters because mutableMapOf
clearly doesn't signal that.Sudhir Singh Khanger
10/16/2019, 4:41 PMWhat do you mean by different types?returns different types for an empty map.mapOf
karelpeeters
10/16/2019, 4:44 PMprintln(mapOf<String, Int>()::class)
println(mapOf<String, Int>("a" to 0)::class)
println(mapOf<String, Int>("a" to 0, "b" to 1)::class)
println(mapOf<String, Int>("a" to 0, "b" to 1, "c" to 2)::class)
//prints:
//kotlin.collections.EmptyMap
//java.util.Collections$SingletonMap
//java.util.LinkedHashMap
//java.util.LinkedHashMap