Apex Collections

Apex Collections

Apex has the following types of collections:

  • Lists
  • Sets
  • Maps

Lists

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.

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 Your_List. Remember, because Apex is strongly typed, you must declare the data type of Your_List as a list of Integer.

List<Integer> Your_List = new List<Integer>();

Sets

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

Use the following syntax for creating a set:

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

The following example creates a set of String. The values for the set are passed in using the curly braces {}.

Set<String> My_String = new Set<String>{'a', 'b', 'c'};

Maps

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.

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'};

For code samples, extensions and more information on collections in salesforce.com click here

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: