1. Create a standard class named NestedMap with the following code:
2. You use it like this:
3. I created this class because I need 2 levels of maps and I got tired off doing all that typing every time I needed to access the data from level 2.
B4X:
'Class: NestedMap
Sub Class_Globals
Private mm As Map
Private nm As Map
End Sub
'Initializes the NestedMap
Public Sub Initialize()
mm.Initialize
nm.Initialize
End Sub
'Puts a key value pair to the map, overwriting the previous item with the same key (if such exists).
'Returns the previous item with this key or null if there was no such item.
Public Sub Put(Key As Object, Value As Object) As Object
Return mm.Put(Key, Value )
End Sub
'Adds a node to the map and returns that node.
Public Sub PutNode(NodeKey As Object, OverwriteIfExists As Boolean) As NestedMap
If OverwriteIfExists = False And nm.ContainsKey(NodeKey) Then
Return nm.Get(NodeKey)
End If
Dim nn As NestedMap
nn.Initialize()
nm.Put(NodeKey, nn)
Return nn
End Sub
'Adds a map containing key value pairs to the NestedMap.
'Note: It will overwrite all key value paris with the ones in the provided map.
Public Sub PutKeyValueMap(KeyValueMap As Map)
mm = KeyValueMap
End Sub
'Checks if the NestedMap contains the given key.
Public Sub ContainsKey(Key As Object) As Boolean
Return mm.ContainsKey(Key)
End Sub
'Checks if the NestedMap contains the given node key.
Public Sub ContainsNode(NodeKey As Object) As Boolean
Return nm.ContainsKey(NodeKey)
End Sub
'Gets the value object that has been assigned to the provided key.
Public Sub Get(Key As Object) As Object
Return mm.Get(Key)
End Sub
'Gets the value object that has been assigned to the provided key. If no such key exists the default value is returned.
Public Sub GetDefault(Key As Object, Default As Object) As Object
Return mm.GetDefault(Key, Default)
End Sub
Public Sub GetKeyValueMap() As Map
Return mm
End Sub
'Gets the node object(NestedMap) that has been assigned to the provided node key.
Public Sub GetNode(NodeKey As Object) As NestedMap
Return nm.Get(NodeKey)
End Sub
'Removes the key value pair from the NestedMap that is identified by the provided key.
Public Sub Remove(Key As Object)
mm.Remove(Key)
End Sub
'Removes the node object from the NestedMap that is identified by the provided node key.
Public Sub RemoveNode(NodeKey As Object)
nm.Remove(NodeKey)
End Sub
'Removes all the NestedMap key value pairs.
Public Sub Clear()
mm.Clear
End Sub
'Removes all the NestedMap nodes.
Public Sub ClearNodes()
nm.Clear
End Sub
'Returns the count of the key value pairs.
Public Sub getSize As Int
Return mm.Size
End Sub
'Returns the count of the nodes.
Public Sub getNodesSize As Int
Return nm.Size
End Sub
'Returns a list of keys from the key value pairs.
Public Sub getKeys() As List
Return mm.Keys
End Sub
'Returns a list of values from the key value pairs.
Public Sub getValues() As List
Return mm.Values
End Sub
'Returns a list of node keys.
Public Sub getNodes() As List
Return nm.Keys
End Sub
2. You use it like this:
B4X:
Dim myMultiLevelMap As NestedMap
myMultiLevelMap.Initialize
myMultiLevelMap.Put("MainMap", "0") ' adds key MainMap with value 0 to the zero level map
myMultiLevelMap.PutNode("Level1", False).Put("Level1Key", "Level1Value") ' adds the node Level1 in which adds the key Level1Key with Level1Value as value
myMultiLevelMap.PutNode("UserId1", False).PutNode("FromCompanyId1", False).PutNode("FromDepartmentId2", False).Put("ClockIn", DateTime.Now)
myMultiLevelMap.PutNode("UserId1", False).PutNode("FromCompanyId12", False).PutNode("FromDepartmentId23", False).Put("ClockIn", DateTime.Now)
myMultiLevelMap.PutNode("UserId2", False).PutNode("FromCompanyId1", False).PutNode("FromDepartmentId1", False).Put("ClockIn", DateTime.Now)
3. I created this class because I need 2 levels of maps and I got tired off doing all that typing every time I needed to access the data from level 2.
Last edited: