iOS Question MAPs > for each but backwarts like step -1

sorex

Expert
Licensed User
Longtime User
Hello,

Is there a way to iterate backwarths over map items?

In B4A I can use a for loop with step -1 combined with .getvalueat and/or .getkeyat but B4i doesn't have these methods.

I want to remove items without getting errors that I'm trying to read none existing map entries due to none updated map limit counters.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
In B4A I can use a for loop with step -1 combined with .getvalueat and/or .getkeyat but B4i doesn't have these methods.
You shouldn't use GetValueAt / GetKeyAt anymore. Especially not by stepping backwards. It will be very inefficient.

Correct solution that will work on all platforms:
B4X:
Dim m As Map = CreateMap("a": 1, "b": 2, "c": 1)

Dim keysToRemove As List
keysToRemove.Initialize
For Each k As String In m.Keys
   If m.Get(k) = 1 Then keysToRemove.Add(k) 'remove all items with value = 1.
Next
For Each k As String In keysToRemove
   m.Remove(k)
Next
 
Upvote 0
Top