Enterprise Systems Programming Fundamentals: Data Structures II - Lecture 5
Collections in Apex
- Composite data types that aggregate multiple types into a single variable.
- Three main types: Lists, Sets, and Maps.
Lists
- Ordered collection of elements of the same type.
- Elements can be of any data type (primitive, collections, custom).
- Lists resize dynamically.
- Syntax:
List<Type> listName = new List<Type>(); - Access and modify elements using square brackets
[] (zero-indexed). - Common methods:
add(), get(), size(), remove(), sort(). - Nested lists allow hierarchical data representation.
Sets
- Unordered collection of unique elements.
- Useful for ensuring no duplicates.
- Elements can be of any data type.
- Cannot directly access elements using an index.
- Syntax:
Set<Type> setName = new Set<Type>(); - Methods:
add(), addAll(), contains(), remove(), size().
Maps
- Collection of key-value pairs where each key is unique.
- Keys and values can be of any data type.
- Syntax:
Map<KeyType, ValueType> mapName = new Map<KeyType, ValueType>(); - Methods:
put(), get(), containsKey(), keySet(), values(), remove(), size(). - Keys are generally case-sensitive.
Apex vs Salesforce Field Data Types
- Apex data types are used within the programming language.
- Salesforce field data types define the structure and constraints of data stored within Salesforce object fields.