https://kotlinlang.org logo
g

Guy Bieber

10/06/2020, 10:25 PM
The mutableMapStateOf does not appear to maintain the order of the map when you iterate as mutableMapOf does. It randomly reorders my tabs. Any recommendations or is this a known bug?
It definitely would be nice to support it. Guess I will get by key in order to fix the problem short term.
g

gildor

10/06/2020, 11:46 PM
a little surprised that mutableMapOf promises to maintain key order
There is no such promise
but considering the contract of
mutableMapOf
It's not a part of contract, it's an implementation detail, but it intentional, to avoid many bugs caused by usage map and expect that it keeps order
j

jim

10/06/2020, 11:49 PM
There is no such promise
@gildor Did you read the contract for
mutableMapOf
? Can you elaborate? Because I'm not sure how else to interpret the docs.
g

Guy Bieber

10/06/2020, 11:56 PM
This is messy to workaround. I have a generic class passing in some enums. Painful.
Here is a quick workaround. Before:
Copy code
mDataModel.tabMap.forEach() {
After:
Copy code
mDataModel.tabMap.toSortedMap().forEach()
Not very efficient, but it works.
g

gildor

10/07/2020, 12:27 AM
Ahh, interesting, yeah, it's contract of mutableMapOf, I was confused with contract of MutableMap itself
c

Chuck Jazdzewski [G]

10/07/2020, 3:55 PM
The state map does not guarantee stable iteration order like the
LinkedHashMap
returned by
mutableMapOf()
does, whose iteration order is insertion order, but uses a doubly-linked list to maintain this order. The map returned by
mutableStateMapOf()
is more similar to
HashMap
which does not guarantee a stable order. It is implemented by using
PersistentMap
from
kotlinx.collections.immutable
which also does not guarantee a stable order.
g

Guy Bieber

10/08/2020, 6:15 PM
Will there be support for sortableStateMapOf?
c

Chuck Jazdzewski [G]

10/09/2020, 3:32 PM
Not before 1.0 releases. BTW, very few map implementations guarantee sorted order. Only tree based (such as red-black) maps typically do that.
2 Views