Hi Guys..
Here is our today's concept..
What is Collection?
Collections are Nothing but Containers by using the
Collections we will store the data we will retrieve the data and we will
manipulate the data.
We are having 3 types of collections in Salesforce.
List is a ordered data, by using the list we will allow
the duplicate values.
Syntax of a
list: list<Datatype> variable =
new list<datatype> ();
Here list elements can be any datatype, either primitive
datatypes (integer, string, date, datetime---etc.), Subject(standard or custom
object) and Collections(list,set Map).
First value in the list always store into index “0” position.
For example:
I am storing multiple values in the list like
‘Karthik’ ,’Kumar’,’Raj’,’Rani’----,karthik will store into index zero position
and kumar will store into index 1 position and Raj will store into index 2
position.
Index 0 index 1 index 2 indexes 3
Karthik Kumar Raj Rani
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.
Ex: list<list<list<list<list<Datatype>>>>>Variable = new
list<list<list<list<list<Datatype>>>>> ();
List Methods:
Add:
Using Add method we will add the values to our list
Ex:
list<string> s = new list<string>{‘Karthik’,’kumar’,’karthik’,’Raj’};
system.debug(‘$$$$$$’+s);
Output: Karthik, kumar, karthik, Raj
Add (index, listElement):
We can add the list value into the list at specified index
position.
Ex: list<string>
s = new list<string>{'Karthik','kumar','karthik','Raj'};
S.add(2,'karthik Training');
System. Debug('$$$$$$'+s);
Output: Karthik, kumar, karthik Training,
karthik, Raj
Add All();
Add all of the elements in the specified list to the list
that calls the method. Both lists must be of the same type.
Ex: list<string> s = new
list<string>{'Karthik','kumar','karthik','Raj'};
System.Debug('$$$$$$'+s);
list<string> s1 = new
list<string>{'IBM’,'TCS','CSC','CTC'};
s1.addall(s);
System.Debug('$$$$$$'+s1);
Output: IBM, TCS, CSC, CTC, Karthik, kumar,
karthik, Raj
Clear ():
By using of the clear method we will clear the values from
the list.
Ex: list<string> s = new
list<string>{'Karthik','kumar','karthik','Raj'};
s.clear();
System.Debug('$$$$$$'+s);
Output : {}(Blank)
Clone ():
Using the clone method we will clone the values from the
list.
Ex: list<string> s = new
list<string>{'Karthik','kumar','karthik','Raj'};
list<string>
s1 = s.clone();
System.Debug('$$$$$$'+s1);
Size ():
Returns the number of elements in the list.
EX: list<string> s = new
list<string>{'Karthik','kumar','karthik','Raj'};
list<string>
s1 = s.size();
System.Debug('$$$$$$'+s1);
Note: There is no limit on Number of items a collection can hold.
But we need to check the heap size .
Set is an UN Ordered data, set will not allow the duplicate
values.
Syntax of a List:
Set<datatype>
variable = new set<datatype> ();
Important methods in
Set:
Add:
By
using add method we will add the values to our list.
Ex: set<integer> s = new
set<integer>{3,5,1,7,3,6,12,15,34};
system.debug(‘$$$$$$’+s);
Output: 1, 3, 5, 6, 7, 12, 15, 34
Add all:
By using add all method we add all values to our set.
Ex:
set<integer> s = new set<integer>{3,5,1,7,3,6,12,15,34} ;
list<integer>
slist = new list<integer>{200,201,202,203,204,205};
s.addall(slist);
system.debug(‘$$$$$$’+s);
Output: 1, 3, 5, 6, 7, 12, 15, 34, 200, 201, 202,203,204,205
Clear ():
By using of clear method we will clear the values from set.
Ex: set<integer> slist = new
set<integer>{200,201,202,203,204,205};
s.clear();
Output:
Clone();
By using the clone method we will clone the values from set.
syntax: set<integer> s = new
set<integer>{1,5,2,4,3,5,8};
set<integer>
s1 = s.clone();
system.debug(‘$$$$$$’+s1);
Output: 1, 2, 3, 4, 5, 8
Size():
Using of size method we will calculate the number of
components in the set.
Ex: set<integer> s = new
set<integer>{1,5,2,4,3,5,8};
system.debug(‘$$$$$$’+s.size());
Output: 6
Map is a combination of key value pairs, map will allow the
duplicate values but it will not allow the duplicate key.
Syntax:
map<key_datatype,value_datatype>
variable = new map<key_datatype,value_datatype>();
Map methods:
Clear ():
By using clear method we will clear the values from Map.
Ex: map<string,integer> s = new
map<string,integer>{‘IND’ => 91,’US’ => 1 ,’UK’ =>2 ,’USSR’
=> 3,’SL’ => 2}
s.clear();
System.debug(‘$$$$’+s);
Keyset():
Using of the key set we will get only the key’s from the map.
Ex: map<string,integer> s = new
map<string,integer>{‘IND’ => 91,’US’ => 1 ,’UK’ =>2 ,’USSR’
=> 3,’SL’ => 2}
System.debug(‘$$$$’+ s.keyset());
Output: IND, SL, UK, US, USSR
Values ():
By using of values
method we will add the values from map
Ex: map<string,integer> s = new
map<string,integer>{‘IND’ => 91,’US’ => 1 ,’UK’ =>2 ,’USSR’
=> 3,’SL’ => 2}
System.debug(‘$$$$’+ s.values());
Output: 91, 1, 2, 3, 2
Interview
questions on Collections:
Q. What is a
Collection? How many types of collections we have?
Ans: Collections
are nothing but container, using of collections we will store the data ,
retrieve the data and manipulate the data.
We are having 3 types of collections in salesforce.
Q. What is the
difference between list and set?
Ans:
- list is a ordered data and list will allow the
duplicate values, first value in the list always store into index
zero position.
- Set is a UN ordered data set will not allow the
duplicate values.