Apex Collections
Apex has the following types of collections:- Lists
- Sets
- Maps
A list is a collection of elements, such as Integers, Strings, objects, or other collections. Use a list when the sequence of elements is important. You can have duplicate elements in a list. It is similar to an array.
The first index position in a list is always zero.
To create a list:
- Use thenewkeyword
- Use theListkeyword followed by the element type contained within<>characters.
List <datatype> list_name
[= new List<datatype>();] |
[=new List<datatype>{value [, value2. . .]};] |
;
List<Integer> Your_List = new List<Integer>();
A set is a collection of unique, unordered elements. It can contain primitive data types, such as String, Integer, Date, and so on. It can also contain more complex data types, such as sObjects.
To create a set:
- Use thenewkeyword
- Use the Set keyword followed by the primitive data type contained within <> characters
Set<datatype> set_name
[= new Set<datatype>();] |
[= new Set<datatype{value [, value2. . .] };] |
;
Set<String> My_String = new Set<String>{'a', 'b', 'c'};
A map is a collection of key-value pairs. Keys can be any primitive data type. Values can include primitive data types, as well as objects and other collections. Use a map when finding something by key matters. You can have duplicate values in a map, but each key must be unique.
To create a map:
- Use the new keyword
- Use theMapkeyword followed by a key-value pair, delimited by a comma and enclosed in <> characters.
Map<key_datatype, value_datatype> map_name
[=new map<key_datatype, value_datatype>();] |
[=new map<key_datatype, value_datatype>
{key1_value => value1_value
[, key2_value => value2_value. . .]};] |
;
Map<Integer, String> My_Map = new Map<Integer, String>{1 => 'a', 2 => 'b',
No comments:
Post a Comment