Collections in Apex


In this blog post we will learn about collections in Apex. There are three different types of collections in apex: List, Set and Map. Collections are composite data types which allow the developer to aggregate, or collect, multiple other types into a single variable. 


 A list (also called an array) is a variable representing an ordered collection of elements which are distinguised by their indices. The index of a list is numeric, and starts at 0 always. 
This means that if you have one item in your list, then that item’s index is 0. If you have two items, the first item’s index is 0, and the second item’s index is 1. This will become clearer and less confusing the more you use it. A list can be a collection of any data type, primitive or otherwise. A list is declared by specifying the type of element it collects.
For example, List<String> myListOfStrings could be used to represent a collection of strings. Therefore, in order to declare a list variable, you would type list, followed by the type collected within < >. In order to retrieve the value of an element at index 0, you can do String retrieved = myListOfStrings[0];
 You can also quickly instantiate (declare and define) a list to be a collection of results from a query.
 For example, if you wanted to have a list of your Account records, you could do something like
 List<Account> myAccountList = [select ID, name from Account]; to create a list containing an unfiltered list of your accounts with the ID and name fields available.

//Create an empty list of String
List<String> my_list = new List<String>();
// Create a nested list
List<List<Set<Integer>>> my_list_2 = new List<List<Set<Integer>>>();



 Sets are similar to lists in that they are collections of elements. However, they are unordered (you cannot access their values by index), and are special because they cannot contain duplicates. If you attempt to add the same value to a set twice, there will still only be one element in the set with that value. This makes sets ideal for maintaing a collection of unique elements, such as IDs or emails, when operating with large data sets. A set is declared the same way a list is, except the set keyword is used instead of list, like so:
 set<String> mySetOfStrings.
Note A set is a collection of unordered elements, and you cannot use an index to retrieve an element like you can with a list. Also, when iterating through the elements in a set, you should not rely on the order being the same.

// Defines a new set with two elements
Set<String> set1 = new Set<String>{'New York', 'Paris'};



 Maps are what are known as “key/value” pairs. They are collections which index their values using a specific key. So where lists are ordered in the order they are built, with the first element being at index 0, the second at index 1, and so on, maps are indexed by a key you specify. 
Both the keys and values in a map can be any data type, meaning they can be primitive, composites, collections, and user defined. 
For example, if you had a map that had a String type as both the key and value, you could add an element to the collection with key “firstName” and a value of “Mike”. Then, when you wanted to retrieve the value of “firstName”, you would use “firstName” as the index. Maps are declared by using the keyword map followed by the type of key and the type of value, like map<key,value>.
 To declare our map containing the first name value, we would then use 

map<String,String> myStringMap.

 As with lists, you can use a quick statement to instantiate a map to the results of a SOQL query, which would map a record to its corresponding ID. 
For example, with the same Account query as used in the list example:

 map<ID, Account> myAccountMap = new map<ID, Account>([select ID, name from Account]);  //would create a map of your Account records indexed by their corresponding IDs. 


Map<String, String> country_currencies = new Map<String, String>();
Map<ID, Set<String>> m = new Map<ID, Set<String>>();

Above blog post gives you complete information related to Collections in Apex.

To learn More


Recent Posts