List #inSalesforce

Collections in Apex - lists, sets, or maps.

There is no limit on the number of items a collection can hold just consider the limit on heap size.


Here we will discuss about Lists -

List is an ordered collection of elements that can be differentiated by their indices.

List elements can be of any data type —

primitive types,

collections, sObjects,

user-defined types,

built-in Apex types

Suppose there's a table:

Index 0     Index 1 Index 2  Index 3  Index 4 Index 5

'Red' 'Orange' 'Yellow' 'Green'      'Blue' 'Purple'


-Lists can contain any collection and can be nested within one another and become multidimensional.

To declare a list, use the List keyword followed by the primitive data, sObject, nested list, map, or set type within <> characters. 

For example:

// Create an empty list of String

List<String> my_list = new List<String>();

// Create a nested list

List<List<Set<Integer>>> my_list_2 = new List<List<Set<Integer>>>();


-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


-Array Notation for One-Dimensional Lists:

To declare and reference list elements, you can user array notation.

Example:

To declare a one-dimensional list of primitives or objects by following the data type name with the [] characters:

These two statements are equivalent to the next one:

List<String> colors = new String[1];

String[] colors = new String[1];

String[] colors = new List<String>();


-To reference an element of a one-dimensional list, you can also follow the name of the list with the element's index position in square brackets as in the following example:

colors[0] = 'Green';

Please note, all lists are initialized to null. Lists can be assigned values and allocated memory using literal notation.

List<Integer> ints = new Integer[0]; //Defines an Integer list of size zero with no elements

List<Integer> ints = new Integer[6]; //Defines an Integer list with memory allocated for six Integers

You can sort list elements and the sort order depends on the data type of the elements.


Sample Example:

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

justNumbers.add(1);

justNumbers.add(2);

justNumbers.add(3);

System.debug(justNumbers);


Visualhttps://www.youtube.com/watch?v=22xeaODilD8


Reference:

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_collections_lists.htm

https://developer.salesforce.com/docs/atlas.en-us.238.0.apexref.meta/apexref/apex_methods_system_list.htm

https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_list.htm

Comments