Beginner 25 min read

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
  1. List<String> customerNames = new List<String>(); — List<String> declares a list that only holds String values — Apex enforces this type at compile time.
  2. customerNames.add('Amara'); — add() appends to the end.
  3. customerNames.add('Ben'); — appends a second value.
  4. customerNames.add('Amara'); — unlike a Set (next module), a List happily allows the same value more than once.
  5. (blank line)
  6. System.debug(customerNames.size()); — logs 3 — 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']
  1. List<String> customerNames = new List<String>{'Amara', 'Ben', 'Carla'}; — a list built with three starting values.
  2. (blank line)
  3. System.debug(customerNames[0]); — indexes start at 0, exactly like the List<Integer> quantities examples back in Module 7's loops — logs 'Amara'.
  4. System.debug(customerNames[2]); — logs 'Carla', the third item.
  5. (blank line)
  6. customerNames.remove(1); — remove(index) deletes the item at that position and shifts everything after it down by one — removes 'Ben'.
  7. 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
  1. List<Integer> scores = new List<Integer>{50, 20, 90}; — a list built in this specific order.
  2. System.debug(scores); — logs [50, 20, 90] — exact insertion order, unchanged. A List always keeps items in the order they were added (or explicitly sorted, covered later in this module) — the key trait that distinguishes it from Set, 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.

Salesforce: ○ Not connected Connect
APEX

Execution Results

Run your code to see the result.

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

Salesforce: ○ Not connected Connect
APEX

Execution Results

Run your code to see the result.

Lists Quiz

1. Does a List allow duplicate values?

Log in to submit the quiz and save your score.

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.