Routine name: AddToMap
Description: Adds or updates items to a Map or creates and fills a new map.
Dependencies: None
Code:
Example:
Note: This arises because I envy the CreateMap B4J function.
Tags: Map, Collection, Array, List
Description: Adds or updates items to a Map or creates and fills a new map.
Dependencies: None
Code:
B4X:
' Adds or updates items to a Map or creates and fills a new map.
' To create a new Map, pass Null for the ExistingMap parameter.
' KeyList and ValueList must be the same size.
Sub AddToMap(KeyList As List, ValueList As List, ExistingMap As Map) As Map
Private mapResult As Map : mapResult.Initialize
If ExistingMap.IsInitialized Then
mapResult = ExistingMap
End If
For i = 0 To KeyList.Size - 1
mapResult.Put(KeyList.Get(i), ValueList.Get(i))
Next
Return mapResult
End Sub
Example:
B4X:
Private MyMap As Map
' Creates a new map.
MyMap = AddToMap(Array As String("One", "Two", "Three"), Array As String("Une", "Deux", "Trois"), Null)
LogWriteMap(MyMap)
' Adds items to the map
MyMap = AddToMap(Array As String("Four", "Five", "Six"), Array As String("Quatre", "Cinq", "Six"), MyMap)
LogWriteMap(MyMap)
'__________________________________________
Sub LogWriteMap(MapToWrite As Map)
Log(CRLF & "*** Map content:")
For i = 0 To MapToWrite.Size -1
Log(MapToWrite.GetKeyAt(i) & " : " & MapToWrite.GetValueAt(i))
Next
End Sub
Note: This arises because I envy the CreateMap B4J function.
Tags: Map, Collection, Array, List