Activity Forums Salesforce® Discussions What is List Class in Salesforce?

  • Avnish Yadav

    Member
    July 17, 2018 at 1:06 pm

    Hello Madhulika,

    List Class is a collection type class in Salesforce, which contains all the list method and constructors.

    Thanks.

  • Prafull

    Member
    July 17, 2018 at 1:43 pm

    Hi,

    List method is used for all instance methods. This operates on any particular instance of list.
    For example:-
    MyFirstList.clear();

    In the above example, I am clearing all element of the MyFirstList instance.

    Below is the list of all related constructors and method of the List Class:-

    Constructor

    • List()
      Creates a new instance of the List class. A list can hold elements of any data type T.
    • List(listToCopy)
      Creates a new instance of the List class by copying the elements from the specified list. T is the data type of the elements in both lists and can be any data type.
    • List(setToCopy)
      Creates a new instance of the List class by copying the elements from the specified set. T is the data type of the elements in the set and list and can be any data type.

    Method

    • add(listElement)
      Adds an element to the end of the list.
    • add(index, listElement)
      Inserts an element into the list at the specified index position.
    • addAll(fromList)
      Adds all of the elements in the specified list to the list that calls the method. Both lists must be of the same type.
    • addAll(fromSet)
      Add all of the elements in specified set to the list that calls the method. The set and the list must be of the same type.
    • clear()
      Removes all elements from a list, consequently setting the list's length to zero.
    • clone()
      Makes a duplicate copy of a list.
    • contains(listElement)
      Returns true if the list contains the specified element.
    • deepClone(preserveId, preserveReadonlyTimestamps, preserveAutonumber)
      Makes a duplicate copy of a list of sObject records, including the sObject records themselves.
    • equals(list2)
      Compares this list with the specified list and returns true if both lists are equal; otherwise, returns false.
    • get(index)
      Returns the list element stored at the specified index.
    • getSObjectType()
      Returns the token of the sObject type that makes up a list of sObjects.
    • hashCode()
      Returns the hashcode corresponding to this list and its contents.
    • indexOf(listElement)
      Returns the index of the first occurrence of the specified element in this list. If this list does not contain the element, returns -1.
    • isEmpty()
      Returns true if the list has zero elements.
    • iterator()
      Returns an instance of an iterator for this list.
    • remove(index)
      Removes the list element stored at the specified index, returning the element that was removed.
    • set(index, listElement)
      Sets the specified value for the element at the given index.
    • size()
      Returns the number of elements in the list.
    • sort()
      Sorts the items in the list in ascending order.

    Hope this will help you to understand List Class.

    Thanks!

  • shariq

    Member
    September 18, 2018 at 1:35 am

    Hi,

    Adding one more point -

    Using Array Notation for One-Dimensional Lists

    When using one-dimensional lists of primitives or objects, you can also use more traditional array notation to declare and reference list elements. For example, you can declare a one-dimensional list of primitives or objects by following the data type name with the [] characters:

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

    These two statements are equivalent to the previous:

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

    String[] colors = new String[1];

    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. For example:

    colors[0] = 'Green';

    Even though the size of the previous String array is defined as one element (the number between the brackets in newString[1]), lists are elastic and can grow as needed provided that you use the List add method to add new elements. For example, you can add two or more elements to the colors list. But if you’re using square brackets to add an element to a list, the list behaves like an array and isn’t elastic, that is, you won’t be allowed to add more elements than the declared array size.

    All lists are initialized to null. Lists can be assigned values and allocated memory using literal notation. For example:

    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

     

    Hope this helps.

  • Parul

    Member
    September 18, 2018 at 3:10 am

    A list is an ordered collection of elements that are distinguished by their indices. List elements can be of any data type—primitive types, collections, sObjects, user-defined types, and built-in Apex types.

    Lists can contain any collection and can be nested within one another and become multidimensional. For example, you can have a list of lists of sets of Integers. A list can contain up to four levels of nested collections inside it, that is, a total of five levels overall.

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

    Ex:

    // 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>>>();

     

    Thanks

Log In to reply.

Popular Salesforce Blogs