Collections are a variable data type which can store multiple records.


The most important collection type(?). It has a certain syntax and some system methods are made especially for lists. Lists are the same as “arrays” in Java.

*List index always starts with 0.

Here is the syntax for declaring a list. It will contain a list of strings.

List<string> ListOfCities = new List<string>();
System.debug('Value Of ListOfCities'+ListOfCities);

Notice that there are no values mentioned in the list in the example above. Optionally, you can add initial values to the list when you declare it like this. This is done as follows:

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

So you basically replace the ‘()’ with a number of strings within curly brackets.

Apparently, you can create a nested list. I’m not sure what this would be like or what it would be used for.

Methods for lists

  • size()
  • add()
  • get()
  • clear()
  • set()
// Add element to the list using add method
ListOfStatesMethod.add('New York');
// Get the element at the index 0
String StateAtFirstPosition = ListOfStatesMethod.get(0);
// set the element at 1 position
ListOfStatesMethod.set(0, 'LA');
// Remove all the elements in List


A collection type in which the records are unordered. Cannot have duplicate records.

It is declared with much the same syntax as lists:

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

Methods for Sets

  • add()
  • remove()
  • contains()
// Adds an element to the set
// Define set if not defined previously
Set<string> ProductSet = new Set<string>{'Phenol', 'Benzene', 'H2SO4'};
System.debug('Set with New Value '+ProductSet);

// Removes an element from set
System.debug('Set with removed value '+ProductSet);

// Check whether set contains the particular element or not and returns true or false
System.debug('Value of Set with all values '+ProductSet);


Collection type where each entry contains a matched ‘key’ and ‘value’. Key and value can be of any data type.

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

Map methods

  • put()
  • .assert()?
  • containsKey()
  • get()
  • keySet
// 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.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);


So, here we covered three types of collections:

  • Lists. Ordered collection starting with 0.
  • Sets. Unordered collection.
  • Maps. Unordered collection of key-value pairs.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s