Lists
By the end of this lesson, you'll be able to:
- Declare and initialize a List of a specific type
- Add, access by index, and remove items from a List
- Recognize that a List preserves insertion order and allows duplicates
Prerequisites: "What Is a Collection?"
Declaring and adding to a List
List<String> customerNames = new List<String>();
customerNames.add('Amara');
customerNames.add('Ben');
customerNames.add('Amara'); // duplicates are allowed
System.debug(customerNames.size()); // 3
List<String> customerNames = new List<String>();—List<String>declares a list that only holdsStringvalues — Apex enforces this type at compile time.customerNames.add('Amara');—add()appends to the end.customerNames.add('Ben');— appends a second value.customerNames.add('Amara');— unlike aSet(next module), aListhappily allows the same value more than once.- (blank line)
System.debug(customerNames.size());— logs3— all three additions counted, including the duplicate.
Accessing and removing by index
List<String> customerNames = new List<String>{'Amara', 'Ben', 'Carla'};
System.debug(customerNames[0]); // 'Amara' — first item, index 0
System.debug(customerNames[2]); // 'Carla'
customerNames.remove(1); // removes 'Ben'
System.debug(customerNames); // ['Amara', 'Carla']
List<String> customerNames = new List<String>{'Amara', 'Ben', 'Carla'};— a list built with three starting values.- (blank line)
System.debug(customerNames[0]);— indexes start at0, exactly like theList<Integer> quantitiesexamples back in Module 7's loops — logs'Amara'.System.debug(customerNames[2]);— logs'Carla', the third item.- (blank line)
customerNames.remove(1);—remove(index)deletes the item at that position and shifts everything after it down by one — removes'Ben'.System.debug(customerNames);— logs['Amara', 'Carla'].
Order is preserved
List<Integer> scores = new List<Integer>{50, 20, 90};
System.debug(scores); // [50, 20, 90] — exact insertion order, unchanged
List<Integer> scores = new List<Integer>{50, 20, 90};— a list built in this specific order.System.debug(scores);— logs[50, 20, 90]— exact insertion order, unchanged. AListalways keeps items in the order they were added (or explicitly sorted, covered later in this module) — the key trait that distinguishes it fromSet, which makes no order guarantee at all.
Exercise
Given a List<Contact>, build a Set<String> containing the unique email domains (the part after '@') across all contacts.
Show hint
Loop over the contacts, split each email on '@', add the domain to a Set.
Exercise
Create a List<String> of three fruit names, remove the second one, and debug the size of the remaining list.
Show hint
fruits.remove(1); then fruits.size();
Lists Quiz
My Notes
Log in to keep private notes on this lesson.
Questions about this lesson
No questions yet — be the first to ask.
Log in to ask a question about this lesson.
Summary
List preserves order and allows duplicates. Set enforces uniqueness. Map stores key-value pairs and is the standard way to look up records by Id without a second SOQL query.