How to initialize List/Set values by construction in apex?

  • I have different object snippet at many places (shown below) and I want below 4 lines to be converted into a single line. How we can do it in Apex ?

    Map<String, List<Configuration__c>> mapConfig = new Map<String, List<Configuration__c>>();
    Configuration__c c = // detail coming from DB here
    List<Configuration__c> tempList = serviceTypeFieldsMap.get(detail.ServiceType__c);
    mapConfig.put(detail.ServiceType__c, tempList);

    I tried many things, but did not worked out.

    mapConfig.put(detail.ServiceType__c, serviceTypeFieldsMap.get(detail.ServiceType__c).add(c));

    What are you trying to achieve here? You want a map containing a single value?

    Is there a way to do this in one line of code?

  • The problem is that the Apex list add method returns void a Boolean rather than the list itself. So it isn't possible to append to the list and initialize in one line.

    You can save one line of code like this using the map initialization syntax:

    Configuration__c c = ...
    List<Configuration__c> tempList = serviceTypeFieldsMap.get(detail.ServiceType__c);
    Map<String, List<Configuration__c>> mapConfig = new Map<String, List<Configuration__c>>{c.ServiceType__c => tempList};

    In the general case you can construct and initialise collections in a single line like this:

    List<String> l = new List<String>{'a', 'b', 'c'};
    Set<String> s = new Set<String>{'a', 'b', 'c'};
    Map<String, String> m = new Map<String, String>{'a' => 'x', 'b' => 'y', 'c' => 'z'};

    including nested combinations such as this one:

    Map<String, List<String>> m = new Map<String, List<String>>{
        'a' => new List<String>{'p', 'q', 'r'},
        'b' => new List<String>{'s', 't', 'u'}


    Based on your comment, if both mapConfig and serviceTypeFieldsMap are defined at the class level, perhaps the best you can do is move the code to a method:

    private void someMethod() {
        if (...) addToConfig(detail1.ServiceType__c, c1);
        if (...) addToConfig(detail2.ServiceType__c, c2);
        if (...) addToConfig(detail3.ServiceType__c, c3);
    private void addToConfig(String key, Configuration__c c) {
        List<Configuration__c> tempList = serviceTypeFieldsMap.get(key);
        mapConfig.put(key, tempList);

    Apex list add method returns void rather than the list itself - This statement not correct. list.add() will always return Boolean value. Also mapConfig is initialize at class level and based on each condition satisfy, I populate the map. Above code's last line not useful.

    @Kanika You are right about the `add` method; I had just glanced at the documentation which is wrong.

  • Yes, you can do this as a single statement (which is what I assume you mean by line), however I would not recommend using this in actual code:

    Map<String, List<Configuration__c>> mapConfig = new Map<String, >{
        detail.ServiceType__c => 
            serviceTypeFieldsMap.get(detail.ServiceType__c).add(/*Whatever you assign to c here*/) == null ?
                serviceTypeFieldsMap.get(detail.ServiceType__c) : 

    In reference to the comment above, List add seems to return an Object which is always null.

License under CC-BY-SA with attribution

Content dated before 7/24/2021 11:53 AM

Tags used