• Dear Cerberus X User!

    As we prepare to transition the forum ownership from Mike to Phil (TripleHead GmbH), we need your explicit consent to transfer your user data in accordance with our amended Terms and Rules in order to be compliant with data protection laws.

    Important: If you accept the amended Terms and Rules, you agree to the transfer of your user data to the future forum owner!

    Please read the new Terms and Rules below, check the box to agree, and click "Accept" to continue enjoying your Cerberus X Forum experience. The deadline for consent is April 5, 2024.

    Do not accept the amended Terms and Rules if you do not wish your personal data to be transferred to the future forum owner!

    Accepting ensures:

    - Continued access to your account with a short break for the actual transfer.

    - Retention of your data under the same terms.

    Without consent:

    - You don't have further access to your forum user account.

    - Your account and personal data will be deleted after April 5, 2024.

    - Public posts remain, but usernames indicating real identity will be anonymized. If you disagree with a fictitious name you have the option to contact us so we can find a name that is acceptable to you.

    We hope to keep you in our community and see you on the forum soon!

    All the best

    Your Cerberus X Team

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

Pixel_Outlaw

New member
Joined
May 10, 2018
Messages
1
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>> ?
 
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)
 
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 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.
 
Back
Top Bottom