Apex has the three types of collections:
1.List 2.Set 3.Map
There is no limit for the number of items a collection can hold. But, there is a general limit on heap size.
Lists: Lists are the mostly widely used collection in Salesforce. A List is a collection of primitives, user-defined objects, sObjects, Apex objects or other collections . Use a List when the sequence of elements is important. You can also have duplicate elements in a List. List are zero-based so the first element in the List is always 0.
To declare a list, use the List keyword followed by the primitive data, sObject, nested list, map, or set type within <> characters. For example:
List<String> my_list = new List<String>(); // Create an empty list of String
List<List<Set<Integer>>> my_list_2 = new List<List<Set<Integer>>>(); // Create a nested list
To access elements in a list, use the List methods provided by Apex.For example:
List<Integer> myList = new List<Integer>(); // Define a new list
myList.add(47);//Adds a second element of value 47 to the end of the list
Integer i = myList.get(0);// Retrieves the element at index 0
myList.set(0, 1); // Adds the integer 1 to the list at index 0
myList.clear(); // Removes all elements from the list
Examples: List<Integer> any = new List<Integer>();
List<String> some = new List<String>{‘Test’, ‘Pss’, ‘Kss’};
// Create a List of Project records from a SOQL query
List<Project__c> project = [SELECT Id, Name, Technology__c FROM Project__c LIMIT 20];
List Methods
List methods check out the docs
Add: This method is used to Add the values to the list.
Example:
List<String> Status =new List<String>();
Status.add(‘Open’);
Status.add(‘Close’);
Status.add(‘Pending’);
List<String> Status =new List<String>{‘Open’,’Close’,’Pending’}
Set : A set is a collection of unique, unordered elements. It can contain primitive data types(String, Integer, Date, etc) or sObjects. If you need ordered elements use a List instead. Sets used to store collections of IDs that you want to use in a SOQL query.
The basic syntax for creating a new Set is:
Set<String> accset = new Set<String>();
Set<String> someset =new Set<String>{‘Prem’, ‘Sem’};
Set Methods
Set methods check out the docs
Map: A Map is a collection of key-value pairs. Keys can be any primitive data type while values can include primitives, Apex objects, sObjects and other collections. Use a map when you want to quickly find something by a key. Each key must be unique but you can have duplicate values in your Map.
The basic syntax for creating a new Map is:
Map<datatype,datatype> Mymap = new Map<datatype,datatype>();
Map<ID, Set<String>> mymap = new Map<ID, Set<String>>();
Map Methods
Map methods check out the docs