What is it called when you initialize things with =>

The system itself seems to refer to this structure as Map Literal Assignment. Try to execute this flawed syntax:

Map<String, String> m = new Map<String, String> { 'a', 'b' };

And you will get this compile error:

expecting a map literal assignment, found ','


They are often referred to as "hash rockets," though it is doubtful you will find documentation on that. You may find this link helpful, but basically it explains what you already know.

Money quote:

As with lists, you can populate map key-value pairs when the map is declared by using curly brace ({}) syntax. Within the curly braces, specify the key first, then specify the value for that key using =>.

Example:

Map<String, String> MyStrings = new Map<String, String>
{
    'a' => 'b',
    'c' => 'd'.toUpperCase()
};

@Houseman Answering your question, when you declare Map data structure without assigning it value, you simply declare it has follows:

Map<String, Integer> myMap = new Map<String, Integer>();

Where String is key and Integer is value;

Now, at the time of declaration if you want to assign values to the map, than you do as follows:

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

Where values before '=>' are key (String value) and value after '=>' are value assigned to the key in the map.

So basically statements or declaration within curly braces with use of '=>' is used to define key value pair for map.

There are other dynamic ways to do this which you can see in the document below: https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_map.htm#apex_System_Map_constructors

Tags:

Apex