BEST GUIDE: Collection In Salesforce – LIST, SET AND MAP.

collections in salesforce

Hello, Friends, I am very happy to tell you all with this useful information. This post is only for Beginners. And for the experienced persons, this post helps them to refresh the things which they already know.

Collections is a type of variable that can store the multiple numbers of records. For example, List can store multiple number of Account object’s records. Let us now have a detailed overview of all collection types.

Lists

Lists are the first and most important of the three data types and 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 types 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’.

For example

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

List<string> ListOfCountries = new List<string>();
System.debug(‘Value Of ListOfCountries’+ListOfCountries);

 

Declaring the initial values of the list is optional. However, we will declare the initial values here. Following is an example which shows the same.

List<string> ListOfName = new List<string> {‘Nandini’, ‘Pooja’, ‘Divya’};
System.debug(‘Value ListOfName’+ListOfName);

List of Accounts (sObject)

List<account> AccountToInsert = new List<account> (); //This will be null
System.debug(‘Value AccountToInsert’+AccountToInsert);

We can declare the nested List as well. It can go up to five levels. This is called the Multidimensional list.

This is the list of set of integers.

List<List<Set<Integer>>> NestedList = new List<List<Set<Integer>>>();
System.debug(‘value NestedList’+NestedList);

 

The list can contain any number of records, but there is a limitation on heap size to prevent the performance issue and monopolizing the resources.

Methods for Lists

There are methods available for Lists which we can be utilized while programming to achieve some functionalities like calculating the size of List, adding an element, etc.

Must Read: TwoFactor Authentication In Salesforce / Best Salesforce Security Guide.

Following are some most frequently used methods −

  • size()
  • add()
  • get()
  • clear()
  • set()

The following example demonstrates the use of all these methods

/ Initialize the List here
List<string> ListOfNameMethod = new List<string>();
// This statement would give null as output in Debug logs
System.debug(‘Value of List’+ ListOfNameMethod);
// Add element to the list using add method
ListOfNameMethod.add(‘Nandini’);
ListOfNameMethod.add(‘Shetty’);
// This statement would give New York and Ohio as output in Debug logs
System.debug(‘Value of List with new Name’+ ListOfNameMethod);
// Get the element at the index 0
String StateAtFirstPosition = ListOfNameMethod.get(0);
// This statement would give Nandini as output in Debug log
System.debug(‘Value of List at First Position’+ NameAtFirstPosition);
// set the element at 1 position
ListOfNameMethod.set(0, ‘shetty’);
// This statement would give output in Debug log
System.debug(‘Value of List with element set at First Position’ + ListOfNameMethod[0]);
// Remove all the elements in List
ListOfNameMethod.clear();
// This statement would give output in Debug log
System.debug(‘Value of List’+ ListOfNameMethod);

 

You can use the array notation as well to declare the List, as given below, but this is not a general practice in Apex programming

String [] ListOfStates = new List<string>();

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.

For 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);

Methods for Sets

Set does support methods which we can utilize while programming as shown below (we are extending the above example) −

// Adds an element to the set
// Define set if not defined previously
Set<string> ProductSet = new Set<string>{‘Phenol’, ‘Benzene’, ‘H2SO4’};
ProductSet.add(‘HCL’);
System.debug(‘Set with New Value ‘+ProductSet);
// Removes an element from set
ProductSet.remove(‘HCL’);
System.debug(‘Set with removed value ‘+ProductSet);
// Check whether set contains the particular element or not and returns true or false
ProductSet.contains(‘HCL’);
System.debug(‘Value of Set with all values ‘+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.

For 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);

Methods for Maps

Following are a few examples which demonstrate the methods that can be used with Map

// Define a new map
Map<string, string> ProductCodeToProductName = new Map<string, string>();
// Insert a new key-value pair in the map where ‘1002’ is key and ‘Acetone’ is value
ProductCodeToProductName.put(‘1002’, ‘Acetone’);
// Insert a new key-value pair in the map where ‘1003’ is key and ‘Ketone’ is value
ProductCodeToProductName.put(‘1003’, ‘Ketone’);
// Assert that the map contains a specified key and respective value
System.assert(ProductCodeToProductName.containsKey(‘1002’));
System.debug(‘If output is true then Map contains the key and output is:’
   + ProductCodeToProductName.containsKey(‘1002’));
// Retrieves a value, given a particular key
String value = ProductCodeToProductName.get(‘1002’);
System.debug(‘Value at the Specified key using get function: ‘+value);
// Return a set that contains all of the keys in the map
Set SetOfKeys = ProductCodeToProductName.keySet();
System.debug(‘Value of Set with Keys ‘+SetOfKeys);

 

Map values may be unordered and hence we should not rely on the order in which the values are stored and try to access the map always using keys.

Map value can be null. Map keys when declared String is case-sensitive; for example, ABC and abc will be considered as different keys and treated as unique.

Stay tuned to salesforceupdates.com for more updates.

Thank you so much for reading my post. Feel free to ask doubts/Queries in the comment section below, and I’ll come again with an interesting topic about Salesforce So Please Share, Subscribe to our blog for the latest update.

Leave a Reply