Klitos Kyriacou
04/12/2022, 8:06 AMmapOf
unless it has to be a hashmap, in which case use hashMapOf
.
2. Use mapOf
to document to your readers that entry order is important, or hashMapOf
to document that order doesn't matter.Rob Elliot
04/12/2022, 9:35 AMmapOf
is the most general Map creation function.
It's a convenience that mapOf
returns a LinkedHashMap
, it's deliberately typed to return just a Map
- if you need to document that you explicitly rely on insertion order you could use linkedMapOf
.
(Incidentally while it's not in the common Kotlin type system, in practice on all three platforms, JS, JVM & Native, LinkedHashMap
is a subtype of or alias for HashMap
.)Klitos Kyriacou
04/12/2022, 9:45 AMin practice on all three platforms, JS, JVM & Native, LinkedHashMap is a subtype or alias for HashMap.I'm not sure what you mean.
Rob Elliot
04/12/2022, 9:51 AMkotlin.collections.LinkedHashMap : kotlin.collections.MutableMap
, so it's not guaranteed to be a HashMap
in all circumstances.
However, in practice there are only 3 Kotlin platforms, JVM, JS & Native, and on those:
• On the JVM kotlin.collections.LinkedHashMap
is an alias for java.util.LinkedHashMap
, and java.util.LinkedHashMap extends java.util.HashMap
. On the JVM kotlin.collections.HashMap
is an alias for java.util.HashMap
• On JS, class kotlin.collections.LinkedHashMap : kotlin.collections.HashMap
• On native, typealias kotlin.collections.LinkedHashMap = kotlin.collections.HashMap
Klitos Kyriacou
04/12/2022, 10:06 AM