Map is a collection that maps keys to values.
It can also be named hashtable, dictionary or associative array.
Each item is made of a key and value. Access to the value is always done through the key.
Retrieving the key is a fast operation. The size of the map doesn't affect the performance (unlike searching for an item in a large list which requires iterating over all items).
The two main methods are Put and Get. Put adds a key / value pair to the map and Get returns the value based on the key.
The keys are unique. If you put an item with the same key as an existing item then the previous item will be replaced.
You can also use CreateMap as a shorthand:
Iterating over the items is done with the For Each iterator:
Note that in most implementations of maps the order is not preserved. In most use cases the order is not important.
The order in B4A and B4J maps is preserved. This is not the case in B4i.
B4A and B4J Maps include two methods named GetValueAt and GetKeyAt. These methods are only available because of historic reasons (before the For Each block was available). You shouldn't use these methods.
The ContainsKey method is used to quickly test whether there is a key / value item with the given key.
For example:
Note that if you try to get a non-existent item then Null will be returned.
The key type and case (for strings) are important.
For example:
Note that the keys are not restricted to strings and numbers. Any object can be used as a key.
This is very useful for mapping an arbitrary object to another object (similar to the Tag property).
There are two ways to save and load maps.
File.WriteMap / ReadMap - Writes or reads the map from text file. This will only work properly with strings and numeric values.
RandomAccessFile.WriteB4XObject / ReadB4XObject - Can write or read maps with custom items.
It can also be named hashtable, dictionary or associative array.
Each item is made of a key and value. Access to the value is always done through the key.
Retrieving the key is a fast operation. The size of the map doesn't affect the performance (unlike searching for an item in a large list which requires iterating over all items).
The two main methods are Put and Get. Put adds a key / value pair to the map and Get returns the value based on the key.
The keys are unique. If you put an item with the same key as an existing item then the previous item will be replaced.
B4X:
Dim days As Map
days.Initialize
days.Put(1, "Sunday")
days.Put(2, "Monday")
Log(days.Get(1))
B4X:
Dim days As Map = CreateMap(1: "Sunday", 2: "Monday")
Log(days.Get(1))
Iterating over the items is done with the For Each iterator:
B4X:
Dim days As Map = CreateMap(1: "Sunday", 2: "Monday", 3: "Tuesday", _
4: "Wednesday", 5:"Thursday", 6: "Friday", 7: "Saturday")
'iterate over the values
For Each day As String In days.Values
Log(day)
Next
'iterate over both keys and values
For Each dayNumber As Int In days.Keys
Dim day As String = days.Get(dayNumber)
Log($"Day number: ${dayNumber}, value: ${day}"$)
Next
The order in B4A and B4J maps is preserved. This is not the case in B4i.
B4A and B4J Maps include two methods named GetValueAt and GetKeyAt. These methods are only available because of historic reasons (before the For Each block was available). You shouldn't use these methods.
The ContainsKey method is used to quickly test whether there is a key / value item with the given key.
For example:
B4X:
If days.ContainsKey(8) Then ...
The key type and case (for strings) are important.
For example:
B4X:
Dim map1 As Map = CreateMap("A": 1, 2: "B")
Log(map1.Get("a")) 'null
Dim d As Double = 2
Log(map1.Get(d)) 'null
Dim i As Int = 2
Log(map1.Get(i)) 'B
Note that the keys are not restricted to strings and numbers. Any object can be used as a key.
This is very useful for mapping an arbitrary object to another object (similar to the Tag property).
There are two ways to save and load maps.
File.WriteMap / ReadMap - Writes or reads the map from text file. This will only work properly with strings and numeric values.
RandomAccessFile.WriteB4XObject / ReadB4XObject - Can write or read maps with custom items.