Apex Basic Part - 3 Collections
Apex Basic Part - 3 Collections
B
Option B is likely to have a predetermined
arrangement or composition that allows you to easily
select the specific number and color of balls you
need.
100% Predictability
Ease of Selection
Precision
Searching Time
Searching Time
Effort to Search
DATA
Structure
L I S T
List in Apex are ordered collections of
elements.
Elements in a list can be of any data type,
including other lists
7 2 9
Memory
arrangement
xef001 xef002 xef003
Elements are stored in contiguous memory
locations, so Lists have the advantage of
easy access to values through index-based
retrieval
Note
In Apex, you cannot directly obtain the memory location
(address) of a variable like you might do in languages
such as C or C++.
Apex, being a high-level programming language,
abstracts low-level details like memory management to
provide a more secure and platform-independent
environment.
how to iterate a value in the list?
Using a Traditional For Loop
Use a traditional for loop when you need explicit control over
the iteration, such as accessing or modifying the index, or
when you need to modify the collection during iteration.
Index vs size
Index In the context of arrays or lists, an index refers to
the position of an element within the data structure.
Indexing usually starts from 0 in programming
languages. For example, in the list [10, 20, 30], 10 is at
index 0, 20 is at index 1, and 30 is at index 2.
Size: Total number of elements it contain in list.
10 20 30 40 Size - 4
Index 0 1 2 3
how to clone a list?
List<Integer> StockList1 = new List<Integer>{2,4,6,8,10};
List<Integer> StockList2 = new List<Integer>();
StockList2 = StockList1;
StockList2.add(12);
system.debug(StockList1);
>> (2,4,6,8,10,12)
I add 12 in Stocklist2 but it
automatically add 12 in Stocklist1 also
How?
because both lists share the same memory StockList1 is
just a reference of the StockList2 is called shallow copy.
Example - Mirror
>> (2,4,6,8,10)
Deep copy create a separate memory space for the new list
How to check whether the list is initialised or
not?
list <integer> IntStockList = new list<integer>();
system.debug(IntStockList);
>> ( ) 1
list <integer> IntStockList ;
system.debug(IntStockList);
>> Null 2
IntStockList.add(10);
System.NullPointerException: Attempt to de-reference a null
object
3
Best Practies - Always ensures that List is not null, and you can
safely perform operations on it without worrying about null
references
Why List is important in apex ?
Bulkification -
Salesforce Governor Limits impose restrictions on
the number of records and operations that can be
performed in a single transaction.
Lists allow for bulk processing of records,
enabling developers to efficiently work with large
datasets while adhering to these limits.
Bulk processing involves performing operations
on collections of records rather than individual
ones, which helps in optimizing performance and
avoiding governor limit issues.
Data Manipulation -
Lists provide a convenient way to manipulate
and work with collections of data. Developers
can iterate over lists, filter records based on
specific criteria, sort records, and perform
various other operations to meet their business
requirements
S E T
Set is an unordered collection .It cannot allow duplicates.
system.debug(IntStockList[0]);
Ordered
>> 23
system.debug(IntStockset[0]);
Purpose of SET
List<Integer> StockList =
new List<Integer>{2,3,4,5,6,3,4,5};
System.debug(StockSet);
Map<String, Integer>:
This specifies the type of the map being declared. In
Apex, a map is a collection of key-value pairs where each
key maps to a single value.
system.debug(Courses);
>> {}
returns {} (an empty map) is because
you've just initialized Courses and
haven't added any key-value pairs to it
yet
101 Java
102 Python
103 C++
Indexed Access:
Since Maps are indexed by their keys,
accessing an element by its key is faster
compared to iterating through a List to find a
specific element.
Lists require linear search, which takes O(n)
time complexity on average, where n is the
number of elements in the List.
Courses.put(101,'JAVA');
Courses.put(102,'Python');
Courses.put(103,'C++');
courses.put(103,'Salesforce');
103 Salesforce
Note that when you put a new value with an existing key in a
map, it updates the value associated with that key, so the
value for the key 103 is updated from "C++" to "Salesforce".
Also, maps do not allow duplicate keys, so if you put a key
that already exists, it will simply update the associated value
system.debug(Courses.keyset());
>> {101, 102, 103}
if (Courses.containsKey(103)) {
system.debug(Courses.get(103));
}
else{
system.debug('key not available');
}
containsKey method is being used to
check if a specific key (103 in this
case) exists within the Courses
collection or not.
size
Integer size = Courses.size();
system.debug(size);