48

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.

1 Answer 1

79

Simply change the parentheses to braces (and change the values to integers).

Before:

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

After:

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .