Consider that you have two lists. The first is a list of users you need to email. The second is a list of users that you've already emailed. You want to determine if these two lists match - indicating whether or not you've already emailed everyone.
We can check for this type of equality with Apex in two different ways. The first is by using the equals(listToCompare)
instance method. The second is using the ==
operand.
Consider these are your two lists:
List<String> allCharacters = new List<String>{'Astro@sfdc.com', 'Flo@sfdc.com', 'Cloudy@sfdc.com', 'Codey@sfdc.com', 'Ruth@sfdc.com'}; List<String> charactersEmailed = new List<String>{'Astro@sfdc.com', 'Flo@sfdc.com', 'Cloudy@sfdc.com'};
Visually, we know they don't match, but using the equals()
method, our Apex code will determine that too.
List<String> allCharacters = new List<String>{'Astro@sfdc.com', 'Flo@sfdc.com', 'Cloudy@sfdc.com', 'Codey@sfdc.com', 'Ruth@sfdc.com'}; List<String> charactersEmailed = new List<String>{'Astro@sfdc.com', 'Flo@sfdc.com', 'Cloudy@sfdc.com'}; Boolean everyoneEmailed = allCharacters.equals(charactersEmailed); // FALSE everyoneEmailed = allCharacters == charactersEmailed; // FALSE charactersEmailed.add('Codey@sfdc.com'); charactersEmailed.add('Ruth@sfdc.com'); everyoneEmailed = allCharacters.equals(charactersEmailed); // TRUE everyoneEmailed = allCharacters == charactersEmailed; // TRUE
This eventually evaluates to true because the equality is determined by a list's elements being the same and in the same order.
List<String> allCharacters = new List<String>{'Astro@sfdc.com', 'Flo@sfdc.com'}; List<String> charactersEmailed = new List<String>{'Flo@sfdc.com', 'Astro@sfdc.com'} Boolean everyoneEmailed = allCharacters.equals(charactersEmailed); // FALSE everyoneEmailed = allCharacters == charactersEmailed; // FALSE
In this example, the elements are the same, but their order is different. To solve this, we can sort our Lists using the sort()
instance method.
List<String> allCharacters = new List<String>{'Astro@sfdc.com', 'Flo@sfdc.com'}; List<String> charactersEmailed = new List<String>{'Flo@sfdc.com', 'Astro@sfdc.com'} Integer indexOfAstro = charactersEmailed.indexOf('Astro@sfdc.com'); // Evaluates to 1. allCharacters.sort(); charactersEmailed.sort(); indexOfAstro = charactersEmailed.indexOf('Astro@sfdc.com'); // Evaluates to 0. Boolean everyoneEmailed = allCharacters.equals(charactersEmailed); // TRUE everyoneEmailed = allCharacters == charactersEmailed; // TRUE
sort()
will sort each element in ascending order. This means the index of each element may change.
If comparing String elements, the equals()
method is case-sensitive. Meaning if the casing is different between two Lists they will be treated as distinct Lists.
List<String> characterList = new List<String>{'Astro'}; List<String> characterList2 = new List<String>{'astro'}; Boolean charactersMatch = characterList.equals(characterList2); // FALSE
Awesome work! We're done working our way through Lists! I'm so happy you made it through this lesson, you're doing great. Kudos to you! Please don't forget to share feedback using the links on the homepage. 🫶