Wish Map.AddAll & List.Swap functions I'd like to see implemented

Widget

Well-Known Member
Licensed User
Longtime User
Map1.AddAll(Map2)
I don't know why Map doesn't have a built-in AddAll method like List has, to assign the key/values pairs of Map2 to the existing key/values of Map1. Although this is easy enough to do in code, I think it should be part of the language.

B4X:
'Assign the aMapFrom key/values to aMapDest
'If aMapDest does not have the key, it will be added
'If aMapDest already has the key, the value will be replaced
'If aMapFrom is not initialized, nothing is done.
'If aMapDest is not initialized (and aMapFrom is initialized) then it will be initialized.
public Sub MapAddAll(aMapFrom As Map, aMapDest As Map)
    If aMapFrom.IsInitialized Then                                                                        'We don't need to do anything if aMapFrom is not initialized
        If aMapDest.IsInitialized=False Then                                                        'If aDest
            aMapDest.Initialize
        End If
       
        For Each locKey As Object In aMapFrom.Keys
            aMapDest.Put(locKey, aMapFrom.Get(locKey))
        Next
    End If
End Sub


List1.Swap(x,y) or Array1.Swap(x,y) or Map1.Swap(x,y)
This is easy enough to write in code, but it will be too slow if swapping thousands of entries.
It would be much faster if it is implemented in the language itself.
Well, that's my 2 cents worth. Comment as you see fit.
 
Top