Activity Forums Salesforce® Discussions What are the different type of Collections you can have in Salesforce Apex?

  • shariq

    Member
    September 22, 2018 at 4:29 pm

    There are three main types of collections…

    Lists – A list is an ordered collection of elements that are distinguished by their indices. List elements can be of any data type—primitive types, collections, sObjects, user-defined types, and built-in Apex types.
    Sets – A set is an unordered collection of elements that do not contain any duplicates. Set elements can be of any data type—primitive types, collections, sObjects, user-defined types, and built-in Apex types.
    Maps – A map is a collection of key-value pairs where each unique key maps to a single value. Keys and values can be any data type—primitive types, collections, sObjects, user-defined types, and built-in Apex types.

  • Parul

    Member
    September 22, 2018 at 5:04 pm

    The collection is the type variables which can store multiple numbers of records. It can increase and decrease dynamically.

    There are 3 types of collection
    List Collection
    Set Collection
    Map Collection

    A map is a collection of key-value pairs where each unique key maps to a single value. Keys and values can be any data type—primitive types, collections, sObjects, user-defined types, and built-in Apex types.

    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!’);
            }
        }
    }

    Thanks

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos