### Map List Keys and Values

Example listing all Map Keys and Values.
See also B4J HowTo > Map > Map For Each Loop.
' Return a string listing Key:Key,Value:Value separated by CRLF
`Private Sub Map_ToString(m As Map) As String
Dim sb As StringBuilder
sb.Initialize
' Iterate over keys and values
For Each Key As String In m.Keys
Dim Value As String = m.Get(Key)
sb.Append($"Key: ${Key}, value: ${Value}"$).Append("
")
Log($"Key: ${Key} = Value: ${Value}"$)
Next
Return sb.tostring
End Sub`
NOT RECOMMENDED = For To Loop
Dim Params As Map
For i = 0 To Params.Size - 1
Log(i & ":" & Params.GetKeyAt(i) & " = " & Params.GetValueAt(i))
Next
Inline Java For Each Loop (requires JavaObject library)
`Sub Process_Globals
...
Private joMap As JavaObject = Me
...
End Sub`
`Sub MapForEachInlineJava
Dim MyMap As Map = CreateMap("Key1":"Value1", "Key2":"1", "Key3":"True")
joMap.RunMethod("LoopMap", Array(MyMap))
End Sub`
If Java
import java.util.Map;
import java.util.Map.Entry;
//Looping over a map which has strings as values
public static void LoopMap(Map map) {
for (Map.Entry entry : map.entrySet()) {
System.out.println("Key : " + entry.getKey() + " , Value : " + entry.getValue());
}
}
End If

Changed: 2018-05-27 09:40, Category: Map, File: T6C217M1599.htm, ID: 1599