[SalesForce] Declaring a map with String as keys and Integer as values

I'm getting two errors while compiling this snippet:

map <String,Integer> CharToNumber = new map<String,Integer>(); 

    CharToNumber.put('A', 0); 
    CharToNumber.put('B', 1);

Errors are: 1) expecting a right parentheses, found 'A'
2) UnexpectedSyntaxError(loc = RealLoc(startIndex = 42, endIndex = 43, line = 6, column = 19), message = mismatched input 'A' expecting RPAREN))

Now: I know it's not a common situation, but: can I declare a map of (having String as keys and Integer as values) ?

Best Answer

The code you have posted is valid Apex and reasonable. So the problem is either that some surrounding code is interfering - do you have something else called CharToNumber in any casing? - or that you are hitting a bug in the local compiler that is part of the Force.com IDE that you can ignore.

If you just need to initialize the map you can also use this more compact syntax for that:

map <String,Integer> CharToNumber = new map<String,Integer>{
        'A' => 0,
        'B' => 1
        };

that may work-around the problem.

Related Topic