Hello friends, Welcome to sfdcAmplified. Today we will learn about list in Salesforce.
Preface
I will encourage you to read the previous blog post where I have written Datatypes and variables in Apex to understand this blog post more clearly.
Topics to be covered
- What is a collection?
- What is a list?
- Different methods of a list
What is a Collection?
Collections is a type of variable that can store multiple number of records.
There are three types of collection
- List Collection
- Set Collection
- Map Collection
What is a List?
- A list is an ordered collection of elements.
- List elements can be of any data type—primitive types, collections, sObjects, user-defined types, and built-in Apex types
- List allows duplicate value
Lets firstly see the syntax of List
Syntax:
List<datatype> variablename = new List<datatype>();
Lets understand it with help of a Example:
Explanation
In the above image you can see that there is a list named ‘colors’. Values are stored in the below position:
index position 0 = ‘red’
index position 1 = ‘blue’
index position 2 = ‘green’
Syntax
List<String> colors = new List<String>{'red','blue','green'}; system.debug(colors);
Output
Example 2
List<String> bollywoodActresses= new List<String>{'Deepika','Anushka','Alia'}; System.debug('index 0---' + bollywoodActresses[0]); System.debug('index 1---' + bollywoodActresses[1]); System.debug('index 2---' + bollywoodActresses[2]);
Explanation
Here values are stored as,
index postion 0 = Deepika
index postion 1 = Anushka
index position 2 = Alia
In another way, we can assign values dynamically when we are creating a list.
Output
Methods in list
There are few methods available in list
add(list Element)
add(index, list Element)
addAll(from List)
addAll(from Set)
clear()
clone()
remove()
size()
equals(list2)
get()
sort()
hashCode()
isEmpty()
add(ListElement)
List<Integer> myList = new List<Integer>{32,56}; myList.add(47); system.debug(myList);
addAll(fromList)
List<string> bollywoodActress = new List<string>(); bollywoodActress.add('Deepika'); bollywoodActress.add('Kareena'); bollywoodActress.add('Alia'); System.debug(bollywoodActress); List<string> bollywoodActor = new List<string>(); bollywoodActor.add('Salman'); bollywoodActor.add('Ranveer'); bollywoodActor.add('Ranbir'); System.debug(bollywoodActor); bollywoodActor.addAll(bollywoodActress); system.debug('Both list names are '+' '+bollywoodActor+'');
List<string> bollywoodActress = new List<string> {'Deepika', 'Kareena','Alia'}; Set<string> bollywoodActor = new Set<string> {'Salman', 'Ranveer', 'Ranbir'}; bollywoodActress.addAll(bollywoodActor); system.debug(bollywoodActress);
size()
List<string> bollywoodActor = new List<string>(); bollywoodActor.add('Salman'); bollywoodActor.add('Ranveer'); bollywoodActor.add('Ranbir'); System.debug(bollywoodActor); System.debug(bollywoodActor.size());
Output
remove()
List<string> bollywoodActress = new List<string>(); bollywoodActress.add('Deepika'); bollywoodActress.add('Kareena'); bollywoodActress.add('Alia'); System.debug(bollywoodActress); bollywoodActress.remove(2); //remove 2nd position value. System.debug(bollywoodActress);
List<string> bollywoodActress = new List<string>(); bollywoodActress.add('Deepika'); bollywoodActress.add('Kareena'); bollywoodActress.add('Alia'); System.debug(bollywoodActress); bollywoodActress.sort(); //sort in ascending order System.debug(bollywoodActress);
clear()
List<string> bollywoodActress = new List<string>(); bollywoodActress.add('Deepika'); bollywoodActress.add('Kareena'); bollywoodActress.add('Alia'); System.debug(bollywoodActress); bollywoodActresses.clear(); System.debug(bollywoodActress);
get(index)
List<string> bollywoodActress = new List<string>(); bollywoodActress.add('Deepika'); bollywoodActress.add('Kareena'); bollywoodActress.add('Alia'); System.debug(bollywoodActress.get(1));
List<string> firstList = new List<string>(); firstList.add('Deepika'); firstList.add('Kareena'); firstList.add('Alia'); System.debug('firstList---' + firstList); List<string> secondList = new List<string>(); secondList=firstList.clone(); System.debug('secondList----'+ secondList);
equals(List)
List<string> bollywoodActress = new List<string>(); bollywoodActress.add('Deepika'); bollywoodActress.add('Kareena'); bollywoodActress.add('Alia'); System.debug(bollywoodActress); List<string> bollywoodActor = new List<string>(); bollywoodActor.add('Salman'); bollywoodActor.add('Ranveer'); bollywoodActor.add('Ranbir'); System.debug(bollywoodActor); Boolean result; result=bollywoodActor.equals(bollywoodActress); system.debug(result);
hashCode()
Hashing is a fundamental concept of computer science. By definition, a hashcode is a number generated from any object. This is what allows objects to be stored/retrieved quickly in a Hashtable.
Why we use hashcode?
Hashcode is used to increase the speed up searching. Lets understand with help of an example. Suppose we need to search a List for a given value.
Case 1 List not sorted: If list is not sorted then search needs examining each elements of the list.
Case 2 List sorted: If the list is sorted, we can use the binary search, and therefore reduce the worse-case run-time complexity to O(log n). ( What is this? Read Big O Notation to get more clarity)
We could search even faster if we know in advance the index at which that value is located in the list.
There is a magic function that tell us the index for a given value. With this magic function our search is reduced to just one probe, giving us a constant runtime O(1). Such a function is called a hash function . A hash function is a function which when given a key, generates an address in the table.
This method will return an integer as a result by mapping an integer to internal memory address in which object is stored.
It always returns a number for an object.Thus, we say that our hash function has the following properties
- two equal objects will always have the same number
- two unequal objects not always have different numbers
Now that you have got the clear understanding of hashcode, we can go ahead and see the example usecase of hascode
List<string> bollywoodActor = new List<string>(); bollywoodActor.add('Salman'); bollywoodActor.add('Ranveer'); bollywoodActor.add('Ranbir'); System.debug(bollywoodActor); System.debug(bollywoodActor.hashCode()); //Output: 485459500
List<string> bollywoodActor = new List<string>(); bollywoodActor.add('Salman'); bollywoodActor.add('Ranveer'); bollywoodActor.add('Ranbir'); System.debug(bollywoodActor); System.debug(bollywoodActor.isEmpty()); //Output: false
Create a list of your favorite fruits and then add the fruits in the second list. Later remove the first list. So list of your favourite fruits will be in the second list. Let me know in the comment section if you face any issue.
Conclusion
List and all its method are highly important. Please practice all the methods to get complete grasp of this concept.
I hope you enjoy the article. For more of these articles stay tuned!
Get latest updates
If you like the Video then subscribe to Youtube Channel
If you like the Article then subscribe to the Blog
Together we can learn faster
You can join the Whatsapp group
You can join Facebook group
You can join Twitter
You can join Instagram
Reference