Search code examples
stringdictionaryobjectgroovy

Fill Map<String, Object> with two strings


I want to fill my Map with two strings. For my Map I have to use <String, Object>.

Using <String, String> would instantly solve my problem, but that is not possible.

What do I need to change to make it work?:

Map<String, Object> myMap= [:]
myMap."foo" = "Bar"

Solution

  • What do I need to change to make it work?:

    Map<String, Object> myMap= [:]
    myMap."foo" = "Bar"
    

    Nothing really. That code is perfectly valid. You could also do this:

    Map<String, Object> myMap= [:]
    myMap.foo = 'Bar'
    

    Or this:

    Map<String, Object> myMap= [foo: 'Bar']
    

    And all of those will work if you change Map<String, Object> to Map<String, String>.