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 automaticallyMap — 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
Tip: in Apex, new List<String>{'a','b'} builds the same idea.