List and Set in Salesforce

Learn About List and Set in Salesforce

LIST

  • List is an ordered collection.
  • List allows duplicates.
  • We can access list elements with indexes.
  • We can sort the list element with sort attributes.
  • Contains attributes are not available in the list.
  • We can process records that are stored in a list using DML statements.

Let us take an example:-

Example 1:-

Advertisement

Step 1:- Login into your Salesforce org. And open your developer console.

Step 2:- Open the anonymous window by clicking CTRL+E.

Step 3:- Paste the below code in the anonymous window.

List<string> myStringList = new List<string>();
myStringList.add('ABC');
myStringList.add('DEF'); 
myStringList.add('HIJ'); 
myStringList.add('KLM'); 
system.debug(myStringList);

Step 4:- Check the ‘open log’ and click ‘Execute’.

Step 5:- Click on ‘Debug Only’.And you can see the below as result.

Result:- 15:01:14:003 USER_DEBUG [6]|DEBUG|(ABC, DEF, HIJ, KLM)

From the above example, you can observe that using a list we can print the result as given in input.

dont miss out iconDon't forget to check out: Salesforce Opportunity Stages Best Practices

Example 2:-

Step 1:- Login into your Salesforce org. And open your developer console.

Step 2:- Open the anonymous window by clicking CTRL+E.

Step 3:- Paste the below code in the anonymous window.

List<integer>myIntegerList = new List<integer>();
myIntegerList.add(2);
myIntegerList.add(3);
myIntegerList.add(6);
myIntegerList.add(1);
myIntegerList.add(2);
system.debug(myIntegerList);

Step 4:- Check the ‘open log’ and click ‘Execute’.

Step 5:- Click on ‘Debug Only’. And you can see the below as result.

Result:-  15:06:11:003 USER_DEBUG [6]|DEBUG|(2, 3, 6, 1,2)

From the above example you can observe that the list allows duplicates as in the result we can see that ‘2’ is written twice in the Debug.

SET:-

  • Set is unordered.
  • Set does not allow duplicates.
  • Set element can not be accessed with index.
  • Sort attributes are not available for set.
  • Contains attributes are available for the set to search for a particular element in the set.
  • We can not process the records which are not stored in the set using DML statements.

Let us take an Example:-

Example 1:-

Step 1:- Login into your Salesforce org. And open your developer console.

Step 2:- Open the anonymous window by clicking CTRL+E.

Step 3:- Paste the below code in the anonymous window.

Set<string> myStringSet = new Set<string>();
myStringSet.add('Ball');
myStringSet.add('Apple'); 
myStringSet.add('Mango'); 
myStringSet.add('Dog'); 
system.debug(myStringSet);

Step 4:- Check the ‘open log’ and click ‘Execute’.

Step 5:- Click on ‘Debug Only’. And you can see the below as result.

Results:- 15:11:09:003 USER_DEBUG [6]|DEBUG|{Apple, Ball, Dog, Mango}

From the above example, you can observe that the set arranges the value in alphabetical order.

dont miss out iconCheck out another amazing blog by Anshu here: All You Need to Know About SOQL in Salesforce

Example 2:-

Step 1:- Login into your Salesforce org. And open your developer console.

Step 2:- Open the anonymous window by clicking CTRL+E.

Step 3:- Paste the below code in the anonymous window.

Set<integer>myIntegerSet = new Set<integer>();
myIntegerSet.add(2);
myIntegerSet.add(3);
myIntegerSet.add(6);
myIntegerSet.add(1);
myIntegerSet.add(5);
myIntegerSet.add(1);
system.debug(myIntegerSet);

Step 4:- Check the ‘open log’ and click ‘Execute’.

Step 5:- Click on ‘Debug Only’. And you can see the below as result.

Result:- 15:14:28:003 USER_DEBUG [8]|DEBUG|{1, 2, 3, 5, 6}

From the above example, you can observe that the set also arranges the value in numeric order and also does not allow duplicates as we have provided ‘1’ two times but in the debug we get it only once.

Responses

Popular Salesforce Blogs