0% found this document useful (0 votes)
12 views6 pages

Salesforce Collections

Uploaded by

joharsai369
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views6 pages

Salesforce Collections

Uploaded by

joharsai369
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Salesforce Collections

Collection refers to a group of data types that are used to store multiple values together.
Collections are key components of the Apex programming language in Salesforce,
enabling developers to handle groups of related data efficiently.

There are three types of collections in Salesforce:

○ List Collection
○ Set Collection
○ Map Collection

1. List Collection:

○ An ordered collection of elements, where each element is stored in an


index.
○ Lists can contain elements of any data type, including primitives (like
Integer, String), objects (like Account, Contact), or other collections.
○ Lists are dynamic, meaning their size can increase or decrease as
elements are added or removed.

Example:
List<String> fruits = new List<String>();

fruits.add('Apple');
fruits.add('Banana');
fruits.add('Orange');

String firstFruit = fruits[0]; // Retrieves 'Apple'

for (String fruit : fruits) {


System.debug(fruit);
}

N.Veera Raghavamma
Output:

2. Set Collection:

○ An unordered collection of unique elements. This means no duplicates are


allowed in a set.
○ Sets are useful when you need to ensure that the collection contains only
unique values.

Example:

Set<String> fruits = new Set<String>();

fruits.add('Apple');
fruits.add('Banana');
fruits.add('Orange');
fruits.add('Apple'); // Duplicate value

for (String fruit : fruits) {


System.debug(fruit);
}

N.Veera Raghavamma
Output:

3. Map Collection:

○ A collection of key-value pairs. Each unique key maps to a single value.


○ The key-value pairs can be any data type, making maps useful for
creating relationships between data or grouping data for easy retrieval.

Example:

Map<String, Integer> fruitBasket = new Map<String, Integer>();

fruitBasket.put('Apple', 10);

fruitBasket.put('Banana', 5);

fruitBasket.put('Orange', 7);

fruitBasket.put('Orange', 4);

fruitBasket.put('Banana', 7);

Integer appleCount = fruitBasket.get('Apple'); // Retrieves 10

for (String fruit : fruitBasket.keySet()) {

System.debug(fruit + ': ' + fruitBasket.get(fruit));

N.Veera Raghavamma
Output:

comparison of Set, List, and Map collections:-

Feature List Set Map

Definition Ordered An unordered A Collection of Key-Value


Collection of Collection that pairs where each key is
elements that does not allow unique.
allows duplicates. duplicates.

Duplicates Allowed Yes No No(for key),Yes (for


values)

Order of Elements Maintains Unordered(no Keys are


insertion Order. guaranteed order). unordered,values can be
accessed via keys.

Access by Index Yes (e.g List[0]). No No,but access by key is


possible(e.g
map.get(key)).

Common use Storing and Ensuring Associating unique keys


Cases iterating over uniqueness in a with values for quick
ordered data, collection,removin lookups.
possibly with g duplicates.
duplicates

Retrieve Specific By index( e.g Not directly (must By key(e.g map.get(key)).


Element List[0]). iterate).

N.Veera Raghavamma
Feature List Set Map

Null Values Allowed. Allowed(but only Keys:Not


once). recommended Values
:Allowed

Usage in SOQL Can be used with Can be used with IN Not directly usable in
IN clause clauses (e.g for SOQL queries.
unique values).

Performance Good for ordered Beat for checking Best for fast lookups
and indexed data existence(since no and accessing data by
manipulation duplicates are unique keys.
allowed).

Memory May consume more Efficient for unique Memory usage


Efficiency memory with collections. depends on the
duplicates. number of key- value
pairs stored.

Example Type List<String> (e.g Set<String> Map<String,


[‘Apple’,’Banana’]) (e.g,,{‘Apple’,’Banana’ Integer>(e.g,,{‘Apple’=
}). >5, ‘Banana’=>10}).

Key Points:-

1.List:

● An ordered collection that allows duplicates.


● Great for working with bulk data that needs to be iterated or accessed by
index.
● Example: Storing multiple records from a SOQL query.

N.Veera Raghavamma
2. Set:

● An unordered collection that ensures uniqueness.


● Perfect for when you need to store unique data, like ensuring no duplicate
IDs or values.
● Example: Avoiding duplicates while processing data.

3. Map:

● A collection of key-value pairs where each key is unique.


● Ideal for fast lookups and associating data with unique identifiers like IDs.
● Example: Mapping Account IDs to their corresponding Account records.

Why use collections?

● Bulk processing with fewer SOQL queries.


● Avoid governor limits by reducing DML statements.
● Simplifies managing large datasets efficiently.

N.Veera Raghavamma

You might also like