6

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);
tempList.add(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));
2
  • 2
    What are you trying to achieve here? You want a map containing a single value?
    – Adrian Larson
    Jan 5, 2018 at 18:18
  • Is there a way to do this in one line of code?
    – Kanika
    Jan 5, 2018 at 18:18

2 Answers 2

28

The problem is that the Apex list add method returns 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);
tempList.add(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'}
};

PS

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);
    tempList.add(c);
    mapConfig.put(key, tempList);
}
0
1

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) : 
            serviceTypeFieldsMap.get(detail.ServiceType__c)
};

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .