Initialize Map<String, Integer>

  • Is there a way to initialize a Map<String, Integer> without using put method ?

    I saw that for initalize a Map<String, String> :

    private Map<String, String> myMap = new Map<String, String>('a' => 'John', 'b' => 'Mike', 'c' => 'Eddy');
    

    But this doesn't work with my map.

  • Simply change the round brackets:

    private Map<String, String> myMap = new Map<String, String>('a' => 'John', 'b' => 'Mike', 'c' => 'Eddy');
    

    To curly brackets (and change the values to integers):

    private Map<String, Integer> myMap = new Map<String, Integer>{'a' => 1, 'b' => 2, 'c' => 3};
    

    Cool, thanks to Daniel and Keith for the superfast edits :)

    @Remario I'm not sure what you mean by that, your comment is confusing for people. Can you either delete or elaborate?

License under CC-BY-SA with attribution


Content dated before 7/24/2021 11:53 AM

Tags used