Wish Missing Function MAP

mcGeorge

Member
Licensed User
Hi, i often use the methods

"Map.GetKeyAt()" and "Map.GetValueAt()"

in my B4A-Programms.

Is it possible to implement these methods in B4I?

Thanks, George
 

Erel

Administrator
Staff member
Licensed User
These methods will not be added.

The only purpose of these methods in B4A was to allow developers to iterate over the Map items. This was before the For Each loop was available.
A standard Map structure doesn't support these methods so adding them requires a more complicated (and less optimized) structure.

B4X:
For Each k As String In Map.Keys
 Dim v As Object = Map.Get(k)
 ...
Next
This code is faster and doesn't require a custom structure under the hood.
 

Erel

Administrator
Staff member
Licensed User
Ok, but please, do not remove them from B4A ;)
They will not be removed.

But if i need recover a key for some value ?
Hash maps are not suitable for this task. It is better to use two maps in this case.

This task requires linear search:
B4X:
Sub FindKeyForValue(m As Map, v As Object) As Object
 For Each k As Object in m.Keys
   If m.Get(k) = v Then Return k
 Next
End Sub
 

JonPM

Well-Known Member
Licensed User
These methods will not be added.

The only purpose of these methods in B4A was to allow developers to iterate over the Map items. This was before the For Each loop was available.
A standard Map structure doesn't support these methods so adding them requires a more complicated (and less optimized) structure.

B4X:
For Each k As String In Map.Keys
Dim v As Object = Map.Get(k)
...
Next
This code is faster and doesn't require a custom structure under the hood.
The reasoning is logical, however there is one profound reason why you should implement these methods: to maintain the promise of a cross-platform suite. Migrating large projects from B4A to B4i is mostly unobtrusive, however these sort of inconsistencies slow the process.
 

Bryanne Vega

Member
Licensed User
I worked around this issue in the TableView by passing my data to each cell's tag.

Sadly I cant either port directly non UI code because of this.
 

iCAB

Well-Known Member
Licensed User
Can I trust, the loop below to return the keys in the same order as if I used I used B4A, "Map.GetKeyAt(i)" and "Map.GetValueAt(i)" .

B4X:
For Each k As String In Map.Keys
Dim v As Object = Map.Get(k)
...
Next

In other words

B4X:
Dim iLoopIndex As Int = 0
Dim iKeyIndex As Int = 5

For Each k As String In mLocalMap.Keys
     Dim v As Object = mLocalMap.Get(k)
     ...
     If iLoopIndex = iKeyIndex Then
         Exit
    End If
     iLoopIndex = iLoopIndex + 1
Next
Is k = GetKeyAt(5) ??? assuming that map size is > 6
 
Top