Sub topic 11
Nested collections
This chapter assumes you know how to create and use a List, Set, and Map, and that you can read generic type syntax such as Map<String, Integer>.
What a nested collection is
A collection can hold other collections. The element type or the map value type is itself a collection. You declare the full type, and each level has its own rules.
| Type | Meaning |
|---|---|
| `List<List<Integer>>` | Rows of numbers, like a grid |
| `Map<String, Set<String>>` | Each key maps to a set of unique values |
| `Map<Id, List<Contact>>` | Each key maps to an ordered list of records |
| `Map<String, Map<String, Integer>>` | A lookup table inside a lookup table |
Pick each level by the same rules as before. Use a Set where values must be unique, a List where order or duplicates matter, and a Map where you look things up by key.
Reading and writing inner collections
Chain method calls to reach the inner collection. get on the outer collection returns the inner one, and you then call a method on it.
Map<String, Set<String>> skills = new Map<String, Set<String>>();
skills.put('Ada', new Set<String>{'apex', 'soql'});
skills.get('Ada').add('lwc');
System.debug(skills.get('Ada').size()); // 3
Boolean graceKnowsApex = skills.containsKey('Grace')
&& skills.get('Grace').contains('apex');
System.debug(graceKnowsApex); // falseLists nest with chained indexes. The first index picks the row and the second picks the element.
List<List<Integer>> grid = new List<List<Integer>>{
new List<Integer>{1, 2, 3},
new List<Integer>{4, 5, 6}
};
System.debug(grid[1][2]); // 6
grid[0].add(99);
System.debug(grid[0]); // (1, 2, 3, 99)Common errors
Two mistakes cause most problems with nested collections.
- Calling a method on a missing inner collection.
skills.get('Grace')returnsnullwhen the key is absent. Calling.add(...)on it throws aSystem.NullPointerException. CheckcontainsKeyfirst, and create the inner collection if it is missing. - Forgetting that
getreturns a reference. The inner collection you get back is the same object stored in the map. Changing it changes the map's contents. You do not need to callputagain.
Map<String, List<String>> teams = new Map<String, List<String>>();
teams.put('Platform', new List<String>());
List<String> members = teams.get('Platform');
members.add('Ada');
System.debug(teams.get('Platform')); // (Ada)Choosing the inner type
Choose the inner collection to match what you will ask of it. If you only ask "is this value present?", use a Set, because contains does not need a loop. If you must keep the order of the values or allow repeats, use a List. Records returned from a query are usually kept in a List, because you often loop over them in order. The next chapter applies this idea to group records by a key.
Exercise
A Map<String, Set<String>> named skills has no key 'Linus'. What happens when you run skills.get('Linus').add('apex');, and how do you write the call safely?
Show answer
get returns null for a missing key, so calling add on it throws a System.NullPointerException. Write it safely: if (!skills.containsKey('Linus')) { skills.put('Linus', new Set<String>()); } skills.get('Linus').add('apex');