function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
tgk1tgk1 

Use cases for Apex Collections

Hello - I'm trying to learn Apex but I'm having some difficulty understanding the concepts of Apex collections.  Can someone please explain to me a few examples of when using a list, set, or map would be appropriate?  For example what particular scenario would be good to use a list vs. the other collection classes?  Thanks for the help!

sfdcfoxsfdcfox

As you've gathered, each type of collection has specific purposes.

 

A list is an ordered list of objects that may be primitives, objects, or records. You can use the sort() method to sort primitives, but other types can't be sorted. This is considered the most simple type of collection, and is useful when you need to store an ordered list of records, or need a simple array to hold data. A list is iterable, meaning you can use a for loop to iterate over the elements of the array. Example: { 5, 2, 1, 9, 2, 4 }

 

A set is nearly identical to a list with a few important exceptions. First, each item may only be stored in the set once. Attempting to place a duplicate in the set replaces the prior value with the new copy (essentially dropping the duplicate value). It works only with primitives. Finally, the elements can't be referenced by index, so you would consider a set to be an unordered list. You can, however, iterate over all the possible values as you would a list. This is highly useful when you need just one copy of each value, such as ID values. Example: { 1, 2, 4, 5, 9 }

 

A map is called a "hash" or "dictionary" in other languages. It uses a set to store keys, and each key may hold a single value. This key-value pair is unordered, like a set. A map can hold any type of data for the value half of the pair, but can only use primitives for a key, using the same limitations as a set. You can iterate through the key set, and you can iterate through the values, which if it is a normal object that qualifies as a list, can also be indexed when you use map.values(), although the order isn't guaranteed. You would use this primarily for storing records, such as a key-value pair of accounts in an opportunity trigger so you don't have to query the accounts in duplicate or within a loop. Example: { 1 => 'Account 1', 2 => 'Account 2', 4 => 'Account 3' }

 

Finally, it's worth noting that maps and lists can also hold collections in their own right, up to seven levels deep. You can use these to great effect, such as storing a related collection of items with other items that might be hard to store any other way. One example of this might be a set of calendar data, which might be represented as Map<integer,map<integer,map<integer,string>>>, where the first integer would be a year number, the next a week or month number, and the final value a day number, and the ultimate key being a string of some sort.

 

You can combine these collections together both in triggers to minimize script statements and memory usage, and also in Visualforce pages to create interesting views of data. There's plenty of examples of all three types on the forums here, as well as within various blogs all over the Internet.

Shivanath DevnarayananShivanath Devnarayanan

To add to the explanation its always good to use List when you Expect results from an SOQL query, Because there will not be a null execption , just that the Count of the list would be zero

More about List

 

And maps/ Sets are always used to store queried data so that we can avoid multiple SOQL when in a loop etc,

More about Maps

More about Sets

another feature is a Custom Setting (though represented as a list or other generic type) its always used when we need access to re-used values across the organization

More about Custom Settings

Limitations of Custom Settings

 

Hope this helps .. ! happy coding !

tgk1tgk1

Thanks guys!  Really appreciate your help and insights.

SamuelDeRyckeSamuelDeRycke

If you're happy with the posted answers, you might want to accept one as solution :)