-3

I am bit up confused while selecting from Maps and List whilst working with Apex Triggers, can anyone please explain?

closed as too broad by Adrian Larson, Vigneshwaran G, Sergej Utko, Adam Spriggs, Doug B Aug 1 '16 at 15:52

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • depends on purpose. map is key-value , you can retrieve the value based on the key.while list you need to loop into iteration to read the value. – unidha Aug 1 '16 at 8:56
4

Personally I always try to use Maps when working with an update trigger. Especially when I need to check if some field has changed value. Using maps I can loop through all old and new values in one loop, for example:

public static void beforeUpdateHandler(Map<Id, Account> oldMap, Map<Id, Account> newMap) {
    Account oldAcc;
    Account newAcc;
    for (Id accId : oldMap.keySet()) {
        oldAcc = oldMap.get(accId);
        newAcc = newMap.get(accId);
        if (oldAcc.MyCustomField__c != newAcc.MyCustomField__c) {
            System.debug('MyCustomField__c has changed!');
        }
    }
}

In case if I would use Lists, I'd have to make a loop inside a loop:

public static void beforeUpdateHandler(List<Account> oldList, List<Account> newList) {
    for (Account oldAcc : oldList) {
        for (Account newAcc : newList) {
            if (oldAcc.Id == newAcc.Id) {
                if (oldAcc.MyCustomField__c != newAcc.MyCustomField__c) {
                    System.debug('MyCustomField__c has changed!');
                }
            }
        }
    }
}

which is squared as heavy on CPU as just one loop. For example:

You update 100 records. First option would iterate over 100 elements and take the correct one in each iteration.
Second options would iterate 100 times through the oldList and 100 times through the newList inside each of the iterations. As a result it would iterate through 100*100 = 10 000 elements.

For insert triggers it doesn't really matter because you don't have Trigger.old or Trigger.oldMap anyway, so you won't be able to compare what has changed.

4

I guess this question boils down to "When do I use Maps, Sets and Lists". Whether you're using an Apex Trigger or just a process within an Apex class their usage is the same regardless.

This question was answered on the official Salesforce Developer forums here.

In a nutshell:

List

A list is an ordered collection so use list when you want to identify list element based on Index Number.

For example:

List<Contact> contactList = [SELECT Id, Name FROM Contact LIMIT 10]; // Query
System.debug(contact[1]); // Debug the second returned row

Map

A map is a collection of key-value pairs where each unique key maps to a single value. Keys can be any primitive data type, while values can be a primitive, sObject, collection type or an Apex object.

For example:

Map<String, String> countriesToCapitals = new Map<String, String> {
  'England' => 'London',
  'USA' => 'Washington',
  'Netherlands' => 'Amsterdam'
};

String dutchCapital = contriesToCapitals.get('Netherlands'); // Get the value of the key "Netherlands" which sets the String as "Amsterdam".

System.debug(dutchCapital);

I appreciated you didn't ask about sets, but seeing as it's related:

Set

A set is an unordered collection of primitives or sObjects that do not contain any duplicate elements. So, use set if you want to make sure that your collection should not contain Duplicates.

For example:

Set<String> fruitSet = new Set<String>{
    'Apple',
    'Banana',
    'Cherry',
    'Apple'
}

System.debug(fruitSet.size()); // Returns 3 because Apple is a duplicate and is ignored.

As for your second question, it's already been answered at How should I determine whether to use Before or After when writing a trigger?

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