How do I initialize a Map<string, List<MyType>>

Pixel_Outlaw

New Member
Joined
May 10, 2018
The docs aren't clear on Monkey X, I heard this is a continuation of that project.
Can anyone help me initialize a Map<String, List<MyTypeHere>> ?
 

SecondGear

New Member
Joined
Jul 27, 2017
Not sure I understand the question. Something like that maybe?
Code:
    Local myMap:StringMap<List<MyClass>> = New StringMap<List<MyClass>>()
    Local listOne:List<MyClass> = New List<MyClass>()
    listOne.AddLast(New MyClass())
    listOne.AddLast(New MyClass())
    myMap.Set("key for the first list", listOne)
   
    Local listTwo:List<MyClass> = New List<MyClass>()
    listTwo.AddLast(New MyClass())
    listTwo.AddLast(New MyClass())
    myMap.Set("key for the second list", listTwo)
 

dawlane

Active Member
CX Code Contributor
Joined
Jun 21, 2017
If memory serves me right, you cannot use the Map class it directly as it has an abstract compare method. You must therefore extend this class and implement your own compare method.

Here's the CX implementation for convenience classes IntMap, FloatMap and StringMap.
Code:
Class IntMap<V> Extends Map<Int,V>
Method Compare( lhs,rhs )
Return lhs-rhs
End
End

Class FloatMap<V> Extends Map<Float,V>
Method Compare( lhs#,rhs# )
If lhs<rhs Return -1
Return lhs>rhs
End
End

Class StringMap<V> Extends Map<String,V>
Method Compare( lhs$,rhs$ )
Return lhs.Compare( rhs )
End
An example of StringMap use
Code:
Strict

' Custom data type
Class MyClass
    Field key:String
    Field item:Int
 
    Method New(key:String, item:Int)
        Self.key=key
        Self.item=item
    End Method
 
    Method PrintMsg:Void()
        Print "This is item "+PrintVal()+" in the list under the access key of "+key
    End Method
 
    Method PrintVal:Int()
        Return item
    End Method
End Class

' A string map instance to contain a lists of custom data
Global myMap:StringMap<List<MyClass>> = New StringMap<List<MyClass>>()

Function GetList:List<MyClass>(keyNum:Int)
    Return myMap.Get("Key"+keyNum)
End Function

#Rem
    Initialise the string map.
    The key will be Key(num).
#end

Function InitMap:Void()
    ' Generate ten key each with a list containing 10 objects
    For Local k:Int=1 To 10
        Local list:List<MyClass>=New List<MyClass>
        For Local i:Int=1 To 10
            ' New custom data type with values
            Local myClass:MyClass=New MyClass("Key"+k, i)
            ' Add this to the list
            list.AddLast(myClass)
        Next
        ' Add the list to the StringMap under the current key
        myMap.Add("Key"+k, list)
    Next
End Function

' Main entry
Function Main:Int()
    ' Initialise the map
    InitMap()
 
    ' Pick 5 random items from the map and iterate the list to print values
    For Local k:Int = 1 To 5
        Local list:List<MyClass> = GetList(Rnd(10))
        For Local i:MyClass=Eachin list
            i.PrintMsg
        next
    Next
    Return 0
End Function
 
Last edited:

dawlane

Active Member
CX Code Contributor
Joined
Jun 21, 2017
@dawlane CX already ship with these map classes. No need to rebuild them.
I know. But the OP asked for how to initialise, which to me means that he tried to use Map directly. So I posted the CX class implementation which should give he/she/anyone an idea on what to do. The three convenience classes (IntMap,FloatMap,StringMap) should be all anyone needs; unless you are going to use some freaky custom data type as a lookup key.
 
Top Bottom