Skip to main content
Hi FRIENDS,

 

is the following map declaration correct?

 

Map<Integer, List<String>> myMap = new map{1=>'Sales', 2=>'Force', 3=>'Cloud'};

 

Pls clarify.

 

  --  Thanks in Advance  --

 

Krish
2 answers
  1. Nov 28, 2012, 7:15 AM
    Not quite correct. Please see below.

     

    Map<Integer, List<String>> myMap = new Map<Integer, List<String>>{1=> new List<String>{'Sales'}, 2=>new List<String>{'Force'}, 3=>new List<String>{'Cloud'}};

     

    1. After the =, you have new map with no type. I added <Integer, List<String>> to this.

    2. For each Key Value Pair in the Map, you have the wrong datatype. You are attempting to add a string where the expected parameter is a list of string.

    You can also test this easily by opening the developer console in your org. Go to your name in the upper right hand corner > Developer Console > Click in the text box at the top. This will open a text area where you can type in code that can be executed on your org.

     

    Last, just a word of warning, trying to do declarations like this can get complex and make commenting difficult. Consider breaking it up into a couple lines to make future work easier.