Apex Programming

Apex Collections:  collections is a type of variable that can store multiple number 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.

Apex Supports Three Type  of collection:

  1. Lists
  2. Sets
  3. Maps

Lists

Lists hold an ordered collection of objects. Lists in Apex are synonymous with arrays and the two can be used interchangeably.

The following two declarations are equivalent.

The colors variable is declared using the List syntax.

List<String> colors = new List<String>();

Alternatively, the colors variable can be declared as an array but assigned to a list rather than an array.

String[] colors = new List<String>();

You can add elements to a list when creating the list, or after creating the list by calling the add() method. The example shows you both ways of adding elements to a list.

List<String> colors = new List<String> {‘red’, ‘green’, ‘blue’};

List<String> moreColors = new List<String>();

moreColors.add(‘orange’);

moreColors.add(‘purple’);

List elements can be read by specifying an index between square brackets, just like with array elements. Also, you can use the get() method to read a list element. The example also shows how to iterate over array elements.

String color1 = moreColors.get(0);

String color2 = moreColors[0];

// Iterate over a list to read

elements

for(Integer i=0;i<colors.size();i++) {

// Write value to the debug log

System.debug(colors[i]);

}

Apex supports two other collection types: Set and Map.

 

Sets

A set is an unordered collection of objects that doesn’t contain any duplicate values. Use a set when you don’t need to keep track of the order of the elements in the collection, and when the elements are unique and don’t have to be sorted.

The following example creates and initializes a new set, adds an element, and checks if the set contains the string ‘b’:

Set<String> s = new Set<String>{‘a’,’b’,’c’};

// Because c is already a member, nothing will happen. s.add(‘c’);

s.add(‘d’);

if (s.contains(‘b’)) {

System.debug (‘I contain b and have size ‘ + s.size());

}

Maps

Maps are collections of key-value pairs, where the keys are of primitive data types. Use a map when you want to store values that are to be referenced through a key. For example, using a map you can store a list of addresses that corresponds to employee IDs.

Map<Integer,String> empAdd = new Map<Integer,String>();

empAdd.put (1, ‘123 San Francisco, CA’);

empAdd.put (2.’456 Dark Drive, San Francisco, CA’);

System.debug(‘Address for employeeID 2:’ +

empAdd.get(2));

Maps also support a shortcut syntax for populating the collection when creating it.

Map<String,String>myStrings = new Map<String,String>

{ ‘a’=>’apple’,’b’=>’bee’};

System.debug(myStrings.get(‘a’));

Hits: 202

Share Post

By Himanshu Rana

My Name is Himanshu Rana, 23 Years young, born and grow up in Ghaziabad, India. A High Spirited Salesforce Admin, Developer and a Blogger. I currently work at Wakencode Technologies,

Leave a Reply

Your email address will not be published. Required fields are marked *