what are collections in apex and how to use it

Apex has the following types of collections:

  1. Sets
  2. Lists
  3. Maps 
  4. 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 objects.

this topic is covered in 

what are collections in apex and how to use it

To create a set:

  1. Use the Set keyword followed by the primitive data type contained within <> characters
  2. Use the new keyword

 

There below is given syntax for creating a set:

Set<datatype> set_name
[= new Set<datatype>();] |
[= new Set<datatype>{value [, value2. . .] };] |
;

Set<String> My_String = new Set<String>{‘a’, ‘b’, ‘c’,’d’,’e,’f”};
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.
The first index position in a list is always 0.
To create a list in collections in apex

  1.  Use the new keyword
  2. • Use the List keyword followed by the element type contained within <> characters.

there is given below syntax for using list in Apex:

Use the following syntax for creating a list:

List <datatype> list_name
[= new List<datatype>();] |
[=new List<datatype>{value [, value2. . .]};] |
;

The following example creates a list of Integer, and assigns it to the variable My_List. Remember, because Apex is strongly
typed, you must declare the data type of My_List as a list of Integer.
List<Integer> My_List = new List<Integer>();

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 in collections in apex:

  1. Use the new keyword
  2.  Use the Map keyword followed by a key-value pair, delimited by a comma and enclosed in <> characters.Use the following syntax for creating a map:

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. . .]};] |
;

The following example creates a map that has a data type of Integer for the key and String for the value. In this example, the values for the map are being passed in between the curly braces {} as the map is being created.

Map<Integer, String> My_Map = new Map<Integer, String>{1 => ‘a’, 2 => ‘b’, 3 => ‘c,’4=>’d’};

for more resource click here