B4J Question Maps - order of elements

LucaMs

Expert
Licensed User
Longtime User
Note that in most implementations of maps the order is not preserved. In most use cases the order is not important.
The order in B4A and B4J maps is preserved. This is not the case in B4i.
B4A and B4J Maps include two methods named GetValueAt and GetKeyAt. These methods are only available because of historic reasons (before the For Each block was available). You shouldn't use these methods.


If: "The order in B4A and B4J maps is preserved."
why: You shouldn't use these methods (GetValueAt and GetKeyAt)?


Also, can I... safely :) use the class you (Erel) posted here with B4J "thread safe maps"?


Thank you
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
why: You shouldn't use these methods (GetValueAt and GetKeyAt)?
These methods were introduced before the For Each loop was available as a way to iterate over the entries.
There performance can be bad if they are misused.

The correct and simple way to iterate over a Map is with a For Each loop.
Also, can I... safely :) use the class you (Erel) posted here with B4J "thread safe maps"?
No. Or more correctly only if you add locks in all the methods. In that case you should use a regular Map anyway.
 
Upvote 0
Top