Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

260
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda