Learn Apex
Collections in Apex
Read the shapes you will type in real classes, then use the sandbox to see how each type behaves.
Sandbox

In Apex, you group data with Java-style generics: List<T>, Set<T>, Map<K,V>. Choose the structure that matches your job: ordered rows, unique Ids, or fast lookup by key — then wire it in code the same way every time.

A useful habit: query once, fill a Map<Id, …>, then loop without issuing more SOQL inside the loop.

How this looks in Apex

List — order matters

List<String> stages = new List<String>{
    'Prospect',
    'Qualified',
    'Closed'
};
for (String s : stages) {
    System.debug(s);
}

Set — unique Ids

Set<Id> accountIds = new Set<Id>();
for (Opportunity o : opps) {
    accountIds.add(o.AccountId);
}
// duplicates ignored automatically

Map — lookup by key

Map<Id, Account> byId = new Map<Id, Account>(
    [SELECT Id, Name FROM Account WHERE Id IN :accountIds]
);
Account a = byId.get(someId);

What to notice

  • Order is preserved — the first item stays first.
  • Duplicates are allowed — the same value can appear twice.
#Value
0Alpha
1Beta
2Alpha

Tip: in Apex, new List<String>{'a','b'} builds the same idea.