Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

264
Views
is MutableMap in kotlin/javascript or kotlin/native a LinkedHashMap?

As we know, A mutableMap object in kotlin/jvm is actually a LinkedHashMap object, but is it still always keep its order when iterating in kotlin/js or kotlin/native environment?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Short answer: not necessarily, not even on JVM. If you want to rely on LinkedHashMap's behaviour, use it explicitly.


Long answer:

As we know, A mutableMap object in kotlin/jvm is actually a LinkedHashMap object

This is incorrect. MutableMap is an interface, and as such it can be implemented by any sort of implementation on any target (including JVM). It is most likely a bad idea to rely on the insertion order of a MutableMap in general, because iterating on it is not guaranteed by contract to be done in the same order as the insertion order. This is an implementation-dependent behaviour (not target-dependent).

On the JVM, you could use a TreeMap as a MutableMap too, and that wouldn't preserve insertion order:

val m: MutableMap<String, Int> = TreeMap()
m["a"] = 1
m["c"] = 3
m["b"] = 2
println(m) // prints {a=1, b=2, c=3}

Now, the mutableMapOf() top-level function currently returns a LinkedHashMap implementation on all platforms, but it's not part of its contract (it's not in the doc, and the return type is just MutableMap), so you should not rely on this because it's an implementation detail of the function.

LinkedHashMap is defined in the common part of the Kotlin stdlib, so most likely all platforms' actual implementations should respect the contract of LinkedHashMap. So if you really want to preserve insertion order, you should use LinkedHashMap explicitly in your code.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!