List: A List can hold any type of data and is one of the most important types of collection. A list is an ordered collection of any data type such as primitive types, collections, sObjects, user-defined types, and built-in Apex types.
The
Following are the key features of a list collection in salesforce:
· Duplicates and nulls are allowed in a
list collection.
· “List” is the keyword to declare a
list collection.
· In a list collection, the index position
of the first entry is always zero (0).
· An Apex list collection has the
ability to grow dynamically over time.
· In a list collection, the data type
can be both primitive and non-primitive.
· The keyword followed by the data type
has to be used within <> characters to define a list collection.
The syntax for a list is as follows:
List<datatype> listName = new List<datatype>();
List Class Salesforce: All Methods required for a list collection are
stored in the Apex list class.
What are the Methods in List?
In order to use lists in programming
to achieve certain functions, a few methods in salesforce are available.
The following are the list-class methods in salesforce:
§ Add
§ Clone
§ Remove
§ Size
§ Equals
§ Get
§ Set
§ Clear
Add: The Values or items are added to the list using the list.add()
method.
Syntax:
List name.add ();
Example:
List <String> names = new List<String>();
names.add(‘Sfdc’);
Clone: It is the method of making a new record by sing the
details of an existent one. Simply put, it is creating a duplicate record.
Syntax:
NewList = Old list name. clone ();
Example:
New Names = names. Clone ();
Remove: It is the method of removing the specific value of
the list.
Syntax:
List Name.remove(index);
Example:
List<String> names = new List<String>();
names.add(‘Sfdc’);
names.add(‘Telugu’);
names.add(‘Salesforce’);
names. remove (2);
Size: It is the method of finding the number of elements
present in the list.
Listname.size();
Equals: This is the method of defining whether the value is
equal or not. If the list and the specific list are equal, true is returned;
otherwise, false is returned.
Syntax:
result=Listone.equals(Listtwo);
Here, “List two” is being compared with “Listone”. If both
elements are the same, it shows true; if both elements are not the same, then
it shows false.
Get: This is the method that helps to return or find out a
value from a list.
String getlist = list.get(index);
In this syntax, you have to replace “list” with “list name”
and “index” with “index number”.
Set: This is the method that is used to change the element or
value of an index.
List.set (index, value);
The value of the index, which is mentioned in the code, will
be changed to the value that is present in the code.
Example:
If you write names.set (1, Mark);
Here the value at the “index1” of the list of “names” will be
changed to “mark”.
Clear: This is the method that removes or deletes an element
in the list.
Syntax:
list. clear ();
Set Collection
Set is an unordered collection. It is a unique set of values
that do not have any duplicates. If you want no duplicates in your collection,
then you should opt for this collection.
· A set collection dose not have an
index.
· A set collection does not contain any
duplicates or null values.
· Any data types, such as primitive
data types and sObjects, are allowed in a set collection.
· Set collections are not widely used
in salesforce Apex.
· Sets can hold collections that are
nested inside of one another.
· You have to use the set keyword
followed by the primitive data type name within <> characters to declare
a set collection.
Syntax:
Set<datatype> setName = new Set<datatype>();
0 Comments