collection in salesforce

Types of Collections in Apex Salesforce | Explained

In Apex, Collection is a group of individual objects represented as a  unit (unit). Sometimes you need to group multiple items in a single unit. Like a list of names in a Contact list. You group your files in a folder. You have saved phone numbers with some name or to store information based on some key or sorted data set etc. Collections is a special technique that is used almost in every programming language. It came with Collection classes and also came with methods like list types of method, map types of method. Collections can increase and decrease dynamically based on business requirements.

 There are three types of collections:

  1. List 
  2. Map
  3. Set

dont miss out iconDon't forget to check out: Apex Trigger In Salesforce - The Developer Guide

List

List types collection is the most widely used collection in Apex. Lists are used to store data in sequence. It means if you want to store a string into a series we are going to use list. If we want to store numbers into a series we are going to use a list. Any type of data can be stored in a list. The position of the first element in a list always starts with Zero [0].It can increase dynamically at the run. The list can hold duplicate and null values. The reason behind why Lists are so significant is on the grounds that the yield of each SOQL inquiry is a List. One of the important parts of Lists is that they are ordered. The list can be nested.

The following method is used in the list:

  1. Add ()
  2. Remove ()
  3. clear()
  4. clone()
  5. Size()
  6. equals(list2)

Example:

List<String> DemoList = new List<String>();
DemoList.add('Test1'); // add test1 in Demolis.
DemoList.add('test2');
DemoList.remove(2); //remove 2nd position value.

Map

A map is used to store data in key-value pairs where each unique key maps to a single value. Map Keys may be any primitive data type like string, integer, decimal, Id while values can be a primitive, sObject, collection type, or an Apex object (Salesforce objects).

Map keys and values may be of any data type—primitive types, collections, sObjects, user-defined types, and built-in Apex types.

In Map, keys of type String are case-sensitive. The map gives value on the basis of key. In map collection Key is always unique but a value can be duplicated. Use a map when you want to quickly find something by a key

The following method is used in the map:

  1. clear() 
  2. clone() 
  3. containsKey(key) 
  4. deepClone() 
  5. equals(map2) 
  6. get(key) 
  7. getSObjectType()
  8. isEmpty() 
  9. keySet()
  10. put(key, value)
  11. putAll(fromMap) 
  12. putAll(sobjectArray).
  13. remove(key) 
  14. size() 
  15. values()

Example:

Map<integer, string>Mapee= new map<integer, string>();
Mapee.put(1,'[email protected]');// add '[email protected]'in Mapee.
Mapee.put(2,’[email protected]’);
string name= Mapee.get(1); // get [email protected] from map
string name1= Mapee.get(2);

dont miss out iconCheck out another amazing blog by Ayush here: Understanding Metadata API in Salesforce | The Developer Guide

Set

Set is used to store data in multiple numbers of unordered unique records. Set can be nested like a list. Set can not hold duplicate and null values. set are often not used in Salesforce (Apex).

The following method is used in the Set:

  1. Add ()
  2. Remove ()
  3. Size()
  4. Contain ()
  5. equals(list2)

Example:

Set<string> Demoset = new Set<string>{'P1', 'p2', 'p3'};
System.debug('Value of Demoset '+Demoset );

Reference: github

Responses

Popular Salesforce Blogs