iOS Question Different behavior of map in b4i and b4a

schimanski

Well-Known Member
Licensed User
In b4a, each time, I add one key to a map, the new key will add at the end of the map. In b4i, it is another behavior: New keys are added sometimes to the first position, then to the last or in the middle, without any logic (...for me:D). I have tested the chronological order with
B4X:
For Each key As String In Map1.keys   
    Log(key)
Next
For example, when I add the following keys in series to the map:
11155, 211534, 211560, 211533,

in b4a it is: 11155, 211534, 211560, 211533.
in b4i it is: 211560, 211534, 11155, 211533

Is it possible to define the map to add the new keys always at the end of the map?
 

sorex

Expert
Licensed User
do you have a reason to have it in the right order there? you could add an order value to sort on later.
 

schimanski

Well-Known Member
Licensed User
Yes, I need the right order and I hoped, that it should be possible to get a solution for 'last in, last out'. So it is possible to hold the same code in b4a and b4i....
 

Erel

Administrator
Staff member
Licensed User
That is correct the order is not kept: http://www.b4x.com/b4i/help/collections.html#map

There are technical reasons for this.

You can use the attached class to implement an ordered map:
B4X:
Dim map1 As OrderedMap
map1.Initialize
map1.Put("1", "a")
map1.Put("2", "b")
map1.Put("3", "c")
map1.Put("4", "d")
map1.Put("5", "e")
map1.Put("1", "f")
For Each key As String In map1.keys   
   Log(key & ": " & map1.Get(key))
Next
 

Attachments

fbritop

Active Member
Licensed User
Erel,
Is it possible in B4I to attach an imageview to a map key?

I have done it in Android but in iOS there is no chance to retrieve this image after from the map.

Is there any special treament to attach an imageview to a map?. Same thing happends when I put a label. After putting any of these items, if I log the map it outputs only numeric or string values:

B4X:
(read only map) {
    idProducto = 1302;
    isActive = 1;
    isDelete = 0;
    isDiferido = 0;
    isNew = 0;
    isOffer = 0;
    precio = 36900;
    precioFormat = "$ 36.900";
    producto = "Nutra Nuggets Large Breed Puppy";
    unidad = "SACO 15 KG";
}
Thanks
FBP
 

LucaMs

Expert
Licensed User
That is correct the order is not kept: http://www.b4x.com/b4i/help/collections.html#map

There are technical reasons for this.

You can use the attached class to implement an ordered map:
B4X:
Dim map1 As OrderedMap
map1.Initialize
map1.Put("1", "a")
map1.Put("2", "b")
map1.Put("3", "c")
map1.Put("4", "d")
map1.Put("5", "e")
map1.Put("1", "f")
For Each key As String In map1.keys
   Log(key & ": " & map1.Get(key))
Next

Hi, Erel,

do you think I could use this your class also with B4J "thread safe maps" without problems?
(I know, this could not be the right place for this question, but your class is here ;))

P.S. from doc.:
Unlike B4J / B4A Maps, B4i Map does not retain the original order of items.

So in B4J the order should be kept, but I remember some post in which I read the opposite. Well, it's early (2:13 AM), I can search and test :p


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)?
 
Last edited:
Top