Collections

Ratings:
(4)
Views:0
Banner-Img
  • Share this blog:

Collections in Salesforce

Collections:- Collections are the group of similar types Apex has the following types of Collections

  • Lists
  • Maps
  • Sets

There is no limit on the number of items a collection can hold However there is a general limit on heap size.

Lists

A list is an ordered collection of typed primitives SObjects, user-defined objects, Apex objects or collections that are distinguished by their includes.

40 90 50 70  20

0         1           2       3           4

  • The index position of the first element in a list is always ‘0’
  • To declare a list, use the list keyword followed by the primitive data, SObject, nested list, map, or set type within < > characters

List< String > Mylist= new list<string>();

This will store the block of memory at the database

List <List<set<Integer>>>mylist2= new list <list<set<Integer>>>(); Nested List Note:- A list can only contain up to five levels of nested collections inside it. *The following example describes us about the retrieving 100 accounts stored in variable. List<Account> accs= select Id,Name FROM Account limit 100 ;

*To access elements in a list, use the system methods provided by Apex.

Ex:- List<Integer> Li=new list<Integer>(); Define a new List Li.add(47);  adds a second element of value 47 to the end of the list Integer x= Li.get(1);Retrieves the element at index 1. Li.Set(0,73); adds the integer 73 to the list at index o Li.clear();  --->  removes all elements from the list                                 *Go to the Developer console and execute the following code and check the result by going through USER-DEBUG List<String> Li=new List<string>(); Li.add('Anil'); Li.add('sateesh'); Li.add('nagaraju'); System.debug(Li);  à the o/p from this line is

Anail Laxman vissu Ramu Sita

0              2            3         4       5

String name = Li.get (0);
System.debug (name); ---> the o/p from this line is ANIL
Li.set(2, ‘sanjeev’);
System debug (Li);  --->      the o/p from this line is
Li.clear ();   --->   clear all elements in a list
System.debug (li);  ---> the o/p from this line is null.

Note:- In a list, we can add Duplicate values that is duplicate values will be allowed inside a list.

Aspired to become an Salesforce? Explore the post to discover the know-hows on Salesforce Online Training.

Sets

A set is an unordered collection of primitives or SObjects that do not contain any duplicate elements.To declare a set, use the set keyword followed by the primitive data type name within < > characters Ex:-

set <string> s= new set < string > ();
To access elements in a set use the system methods provided by Apex.
<Integer> s=new set <Integer>(); --->   Define a new set
S.add (1);    --->  Adding an element to the set
Boolean b= s.contains(1);    ---> It will returns true when the element ‘1’ contains within the set.
s.remove (1);  ---> Remove the element from the set
*Go to the Developer console and execute the following code and check the result by going through USER-DEBUG();
Set <string> s=new set <string>(); s.add(‘red’);
s.add(‘bule’);
s.add(‘Black’);
s.add('green');
system.debug (s);
This statement gives the o/p below
{Black, blue ,green ,red ]
s.remove ('green'); system.debug (s);
This statement gives the Output as below {Black, blue, red}
s.add ('red'); system.debug(s)

#### This statement gives the o/p as below {black, blue, red} because the set will not allow duplicate values inside it Boolean b= s.contains ('black') ###Returns true when the value 'black' is within set and it returns "False" when the value 'black' is not there within set. *Uniqueness of sobjects is determined by comparing fields             EX:-If we try to add two accounts with the same name to a set,only one is added.

//Create two accounts a1 and a2
Account a1=new account (Name='Test Account');
Account a2=new account (Name='Test Account');
//Add both accounts to the new set
set<Account> acc set = new set <Account > {a1,a2};
system.assertEquals (accset.size(),1);

This statement is used to verify the set only contains one item.

Maps:-

A map is a collection of key-value pairs where each unique key maps to a single value *Keys can be any primitive datatype while values can be primitive, sobject, collection type or Apex object. *To declare a map, use the Map keyword followed by the data types of the value within < > characters             EX:-

Map < string, string> m=new map <string, string> ();
Map<ID, set<string>> m1=new Map<ID, set <string>>();
Map<ID, Map <ID,Account []>> m2=new map<ID,Map<ID,Account[]>>m2;
Similar to lists, map values can contain any collection and can be nested within one another. A map can only contain up to five levels of nested collections inside it.

The following example gives the information about how a map of contain and currencies
Country ---->  Key
currency---> Value
Map<string, string> m= new Map<string> ();
m.put (‘India’, 'Rupee’);
m.put (‘us’, 'Dollar');    Insert a new key value pair in the map
m.put (‘uk’, 'Pounds');
system.debug (m);          the o/p of this line will be
{India= Rupee, us=Dolar, uk= pounds}
string currency = m.get('uk'); àRetrieve a value given a particular key system.debug (currency), à the o/p of this will be pounds
system.debug (m.keyset ()); à Return a set that contains all of the keys in the map.
system.debug (m.containkey ('uk'));

This statement gives the true or false whether for particular key have a value or not. *   EX:- Account a= new Account (Name='capital',Bullingcity='Hyd'); Map<Integer, Account> map1=new map<integer, Account> (); map1.put (1, a); àInsert a new key value pair in the map Map<Integer, Account >map2=map1.clone (); ###Makes a duplicate copy of the map system.debug ((map1.get (1) Billingcity)); ###This statement gives the Billingcity filed value of the value pair that is assigned to the key 1. Hyd

You liked the article?

Like : 0

Vote for difficulty

Current difficulty (Avg): Medium

Recommended Courses

1/15

About Author
Authorlogo
Name
TekSlate
Author Bio

TekSlate is the best online training provider in delivering world-class IT skills to individuals and corporates from all parts of the globe. We are proven experts in accumulating every need of an IT skills upgrade aspirant and have delivered excellent services. We aim to bring you all the essentials to learn and master new technologies in the market with our articles, blogs, and videos. Build your career success with us, enhancing most in-demand skills in the market.


Stay Updated


Get stories of change makers and innovators from the startup ecosystem in your inbox