Set - Introduction
Not Started

Set is a collection in Apex that looks a lot like a list. Let's rip the bandaid off:

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

See what I mean? It looks similar. We declare and initialize sets in the same way as lists, we just substitute the Set keyword instead.

The set type behaves differently, it can't store duplicate values and it's elements are unordered. This means the elements can be rearranged at any time. For the underlying "why" you can read up on Hash functions here. But it's not required for this course.

Because sets are unordered, we lose our ability to access elements by their index:

Set<String> sfdcHeros = new Set<String>{'Astro'}; String firstHero = sfdcHeros[0]; // Compilation Error: Expression must be a List type: Set<String> // Compilation Error: Method does not exist or incorrect signature: void get(Integer) from the type Set<String>

You can create a set from a set or a list:

List<String> sfdcHerosList = new List<String>{'Astro', 'Ruth', 'Flo', 'Codey'}; Set<String> sfdcHerosSet = new List<String>(sfdcHerosList); Set<String> sfdcHerosSet2 = new List<String>(sfdcHerosSet);

Sets are most commonly used when referring to Ids. This can either be sObject Ids or Ids of external systems. A set can hold elements of any datatype, but just like lists, they must all be of the same datatype.

Challenge

Declare a set of strings named userIdSet. Initialize the set with the following strings: Ux001, Ux005, Ux010.