11

Is it possible to create a list of map objects? Here is what I am currently trying:

public List<Map<String, Integer>> slaMap = 
  new Map<String, Integer>[] {
    new Map<String, Integer> {'P1'=>30, 'P2'=>30},
    new Map<String, Integer> {'P1'=>60, 'P2'=>60},
    new Map<String, Integer> {'P1'=>120, 'P2'=>120},
  };

I can accomplish it using this method:

public List<Map<String, Integer>> slaMap = new List<Map<String, Integer>>();
slaMap.add(new Map<String, Integer> {'P1'=>30, 'P2'=>30});
slaMap.add(new Map<String, Integer> {'P1'=>60, 'P2'=>60});
slaMap.add(new Map<String, Integer> {'P1'=>120, 'P2'=>120});

However, I am wondering if it is possible using something similar to the first syntax I listed.

13
List <Map<String, Integer>> a = new List <Map<String, Integer>> {
 new Map<String, Integer>{'p1'=>10, 'p2'=>10}, 
 new Map<String, Integer>{'p1'=>20, 'p2'=>20}
};
  • 1
    You beat me to it. – sfdcfox Aug 21 '13 at 0:49
  • Good luck next time ;) – Boris Bachovski Aug 21 '13 at 0:51
  • 3
    The reason why: you can't construct an array of collections using the [] syntax. Why not? Who knows, but that's the way it's done. Also, lists can't have a terminating comma. – sfdcfox Aug 21 '13 at 0:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.