7.1 Using Set to store data

The concept

The usage of Set is very similar to List. But the elements are unique in Set and the sequence of elements in a Set is not fixed. So we can’t used the list way of list_name[index] way to reference a Set member. Let’s take a look at the code.

Set<Integer> intSet = new Set<Integer>{2, 3, 4};

for(Integer i: intSet)

Your result should be something like this:


A bit explanation

You may wonder, why there is only one 3 in the result. That’s because all the elements in a Set must be unique. If we try to add a new element into Set and the value already exists in original Set, it will automatically get omitted.

We cannot use index number to locate each set member. But we can still iterate through them using for loop.

The usage is very similar to List. But there are still some differences. We will explain more about the differences and the transition between the two in the next post.

Next Post

7.2 The differences between List and Set

Subscribe to Sfdcinpractice

Subscribe to get the latest blogs and tutorials of sfdcinpractice. No spam, no trash, only the awesome posts from sfdcinpractice. 

Leave a Reply

Your email address will not be published / Required fields are marked *