What are Collections in Apex?
An introduction to complex data types
Apex — the Salesforce programming language that is similar to and based on Java — includes both primitive and complex data types. A data type defines the type of value(s) that can be stored in a given structure since Apex is a strongly-typed language.
Primitive types — such as String, Integer, and Decimal — hold a single value. Conversely, Collections are Apex data types that may hold multiple values in a single identifier.
In this article, we’ll introduce the three types of Collections, when to use each, and provide example code to get you started.
Lists
Lists are an ordered collection of similar data types. The List data type is analogous to an array if you come from a Java, PHP, or JavaScript background.
Values within the list can be referenced by their “position in line” called an index. The primary methods (object-related functions) of the list data type are: .get() and .add(). Let’s take a look at an example where we 1) create an empty list of Integers, add two values, and reference the first.List<Integer> scores = new List<Integer>();scores.add(88);
scores.add(100);System.debug(scores.get(0)); // will print 88
A few important notes about the above code snippet:
- The underlying data type of the list values is defined in angle brackets
- the new keyword is used to initialize the list
- Lists begin with an index of 0
If we wanted to shorten up and simplify the example, we could do so by combining the initialization and data entry as well as using square bracket notation to reference the index.List<Integer> scores = new List<Integer>{88,100};System.debug(scores[0]); // will print 88
We want to use lists when we have a group of similar values, which may benefit from being accessed by their individual index.
Sets
Sets are an unordered collection unique values. Sets differ from Lists in that they do not have an index (as they have no explicit order) and the values may not be duplicated.
We will often see sets used to create and maintain an efficient group of values since duplicates are automatically ignored. We can then check the set to see if a particular value exists within it.Set<String> party = new Set<String>{'Cloud', 'Tifa', 'Berit'};Boolean hasCloud = party.contains('Cloud'); // true
Boolean hasAerith = party.contains('Aerith'); // false
Notice that defining a Set is very similar to a List, but we use .contains() to return a boolean true/false based on whether we find a match within the Set. While we did not use it in the example, Sets also have an .add() method to further populate the structure.
Let’s see what happens if we try to duplicate a value in the Set…Set<String> party = new Set<String>();party.add('Cloud');
party.add('Cloud');System.debug(party.size()); // will print 1
Trying to add a duplicate value to the Set does not cause an error, but the duplicate add will simply be ignored.
Maps
Maps are an unordered collection of unique keys, each paired to a value, similar to dictionaries in Python or objects in the JSON data format. We can think of maps as a List where the indexes are not sequential integers.
Map declarations are a little bit different than Lists/Sets because both the identifier and the value require data types. Let’s look at an example of creating an empty map and adding a key-value pair to it.Map<String,Integer> scores = new Map<String,Integer>();scores.put('Cloud', 88);
Instead of .add(), we use .put() and include both the key and the value. If we were to “put” a different value with the same key, the original value is overwritten, hence explaining why we don’t use the word “add”.Map<String,Integer> scores = new Map<String,Integer>();scores.put('Cloud', 88);
scores.put('Cloud', 100);System.debug(scores.get('Cloud')); // will print 100
The uses for maps are wide-ranging, especially when you begin to use Collections as the value data type and leverage unique keys (such as Salesforce object Ids). For example, we could initialize a Map of account ids paired to a List of opportunities.Map<Id,List<Opportunity>> = new Map<Id,List<Opportunity>>();
Conclusion
Getting comfortable with the different Collection types is critical to efficiently working with the complex data relationships in Salesforce. To summarize: Lists are ordered, Sets are unique, and Maps are paired.
Please share your experiences, questions, and feedback below. Follow Code 85 for more plain language programming guides. Thanks for reading!