Activity Forums Salesforce® Discussions What are the different types of collections in Salesforce Apex?

  • shradha jain

    Member
    July 31, 2018 at 5:56 am

    Hello Sanjana,

    The different types of collections in Apex are:

    1. 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.
    2. 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.
    3. 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 14, 2018 at 5:02 am

    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

     

    Thanks

  • shariq

    Member
    September 15, 2018 at 2:01 pm

    Hi,

    Lists
    List can contain any number of records of primitive, collections, sObjects, user defined and built in Apex type. This is one of the most important type of collection and also, it has some system methods which have been tailored specifically to use with List. List index always starts with 0. This is synonymous to the array in Java. A list should be declared with the keyword 'List'.

    Example

    Below is the list which contains the List of a primitive data type (string), that is the list of cities.

     

    List<string> ListOfCities = new List<string>();
    System.debug('Value Of ListOfCities'+ListOfCities);
    Declaring the initial values of list is optional. However, we will declare the initial values here. Following is an example which shows the same.

    List<string> ListOfStates = new List<string> {'NY', 'LA', 'LV'};
    System.debug('Value ListOfStates'+ListOfStates);

    Sets
    A Set is a collection type which contains multiple number of unordered unique records. A Set cannot have duplicate records. Like Lists, Sets can be nested.

    Example

    We will be defining the set of products which company is selling.

    Set<string> ProductSet = new Set<string>{'Phenol', 'Benzene', 'H2SO4'};
    System.debug('Value of ProductSet'+ProductSet);

    Maps
    It is a key value pair which contains the unique key for each value. Both key and value can be of any data type.

    Example

    The following example represents the map of the Product Name with the Product code.

    // Initialize the Map
    Map<string, string> ProductCodeToProductName = new Map<string, string>
    {'1000'=>'HCL', '1001'=>'H2SO4'};

    // This statement would give as output as key value pair in Debug log
    System.debug('value of ProductCodeToProductName'+ProductCodeToProductName);

    Hope this helps.

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos