What is a collection?
A group of elements or similar types of elements is called collection.
There are three types of Salesforce Collection:-
- Map
- Set
- List
Map: Map is key-value pairs.
Syntax:
Map<Key,Value> nameOfMap = new Map<Key,Value>();
Key → is a set.
Value or Values → is a list.
Methods in Map:
- put() → add a key-value pairs.
- get() → retrieve a value for a key.
- keySet() → retrieve all the keys and return type is set.
- values() → retrieve all the values and return type is list.
- size() → return the number of components in the map.
Set: Set is an ordered collection of elements which will not allow duplicate.
Syntax:
Set<Type> NameOfSet = new Set<Type>();
Type → String or Integer or any data type.
Methods in Set:
- add() → to add.
- get() → to get value by index.
List: List is an unordered collection of elements which will allow duplicate.
Syntax:
List<Type> NameOfList = new List<Type>();
Type → String or Integer or any data type.
Methods in List:
- add() → add the values to the list.
- get() → retrieve a value from a list using the index.
- set() → replaces a former value with the value at given index.
- clear() → Remove the elements from the list.
- size() → return the number of elements in the list.
Comments
Post a Comment