Created
July 27, 2016 19:17
-
-
Save AlwaysThinkin/a3b8889c6f0dcc5c8799462055c5abf9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@isTest | |
public class CollectionsAnswers { | |
static testMethod void Lists(){ | |
//LISTS ARE AN ORDERED COLLECTION OF DATA | |
//How to instantiate an List with some pre-defined values | |
String someword = 'thread'; | |
List<String> threeStrings = new List<String>{'rope', 'twine', someword}; | |
//Lists have methods that allow us to evaluate or manipulate them | |
//See https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_list.htm | |
System.assertEquals(3, threeStrings.size()); | |
//List items can be referenced by their "index" number starting with zero | |
System.assertEquals('rope', threeStrings[0]); | |
//How to Instantiate an empty List | |
List<String> noStrings = new List<String>(); | |
System.assert(noStrings.isEmpty()); | |
noStrings.add('rock'); | |
System.assertEquals(1, noStrings.size()); | |
//FIX ME! Using the values in the first List we made, uncomment the two lines below and get them to pass | |
System.assertEquals('rope', threeStrings[0]); | |
System.assertEquals('twine', threeStrings[1]); | |
System.assertEquals('thread', threeStrings[2]); | |
//or | |
System.assertEquals(someword, threeStrings[2]); | |
//Lists can also contain other lists. Think of an Accounts & Contacts Report. | |
List<List<String>> numberNames = new List<List<String>>(); | |
numberNames.add(new List<String>{'zero', 'one', 'two', 'three', 'four'}); | |
numberNames.add(new List<String>{'cero', 'uno', 'dos', 'tres'}); | |
numberNames.add(new List<String>{'ling', 'yi', 'er'}); | |
System.assertEquals(3, numberNames.size()); | |
//FIX ME! Using the data in numberNames uncomment the lines below and get them to pass | |
System.assertEquals(3, numberNames[2].size()); | |
System.assertEquals(4, numberNames[1].size()); | |
//Index notation can be chained to retrieve values in lists of lists | |
System.assertEquals('zero', numberNames[0][0]); | |
//FIX ME! Build the String in the expected values - don't forget to concatenate spaces into your string! | |
System.assertEquals('uno dos tres', numberNames[1][1] + ' ' + numberNames[1][2] + ' ' + numberNames[1][3]); | |
//Creating a List from a SOQL query is very common | |
List<Profile> profs = [SELECT Name FROM Profile]; | |
System.debug('All the Profiles: ' + profs); | |
//Note that even though we did not add ID to the SELECT, it's retreived: | |
System.debug('Profile 1 ID: ' + profs[0].Id); | |
//But any other field must be added to the SELECT to be referenced: | |
//System.debug('THIS WILL NOT WORK!' + profs[0].CreatedDate); | |
//FIX ME! Make the following assertion pass by figuring out a way to count all the Profiles | |
//SOLUTION: the count may vary by org so using system.debug(profs.size()) is an easy way to check | |
System.assertEquals(36, profs.size()); | |
//Alternate method for creating a List: datatype first, followed by [] | |
//This allows you to set a "dimension" that forces the List size to be validated. | |
String[] oneWord = new String[1]; | |
oneWord[0] = 'able'; | |
//oneWord[1] = 'baker';//Won't Work! The List was configured for only 1 item | |
oneWord.add('charlie');//Will Work! The .add() method automatically expands the List | |
system.debug(oneWord); | |
oneWord[1] = 'baker'; | |
System.debug(oneWord); | |
System.assertEquals(2, oneWord.size()); | |
} | |
static testMethod void Sets(){ | |
//SETS ARE AN UNORDERED COLLECTION OF UNIQUE DATA | |
Set<String> planets = new Set<String>{'Mars', 'Earth', 'Pluto'}; | |
System.assertEquals(3, planets.size()); | |
//Sets enforce uniqueness so you don't have to check whether a value is already in a Set | |
Set<String> months = new Set<String>(); | |
months.add('June'); | |
months.add('July'); | |
months.add('June'); | |
System.assertEquals(2, months.size()); | |
//Sets have some similar methods to Lists like add() and some useful others like contains() | |
//See https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_set.htm | |
System.assert(months.contains('July')); | |
//Sets are usually created from other collections like Lists | |
//Here are two methods for adding a List to a Set | |
//1. Add an existing list when instantiating the Set | |
List<ID> list0 = new List<ID>{'00Q3600000CqgXh', '00Q3600000yTEaB','00Q3600000RiGht'}; | |
Set<ID> setA = new Set<ID>(list0); | |
//2. Use a Set Method to add a List (or another Set) | |
List<ID> list1 = new List<ID>{'00Q3600000zUCaR', '00Q3600000QpjNq','00Q3600000RiGht'}; | |
setA.addAll(list1); | |
//FIX ME! Write System Assert methods to confirm that setA contains all the elements from list0 and list1 | |
System.assert(setA.containsAll(list0)); | |
System.assert(setA.containsAll(list1)); | |
} | |
static testMethod void Maps(){ | |
//MAPS ARE NAMED COLLECTIONS OF DATA | |
Map<String, String> fieldValues = new Map<String, String>{'Name' => '2u, Inc.', 'Website' => '2u.com', 'Phone' => '301-892-4350'}; | |
System.assertEquals(3, fieldValues.size()); | |
//Maps are useful to relate something you know, to something you want to know. | |
//Here we map an integer (we know) to a string which holds the Mandarin word (we don't know) for the number. | |
Map<Integer, String> numbers = new Map<Integer, String>(); | |
numbers.put(1, 'Yi'); | |
numbers.put(2, 'Er'); | |
numbers.put(3, 'San'); | |
//Notice how we use "put" instead of "add"? Same purpose, just different name. | |
//See https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_map.htm | |
//The power of maps comes from the get() method which allows us to use the "key", something we know, | |
//to get the "value", something we don't know. | |
System.debug(numbers.get(1)); | |
//FIX ME! Using the data in the numbers map, uncomment the lines below and get them to pass | |
System.assertEquals('Er', numbers.get(2)); | |
System.assertEquals('San', numbers.get(3)); | |
//Maps are divided into Keys and Values, and keyset() and values() allows you to get these collections easily | |
System.debug(numbers.keySet()); | |
System.debug(numbers.values()); | |
//FIX ME! Populate the Set called 'mynumbers' that you can then use to get the assert to pass | |
Set<Integer> myNumbers = new Set<Integer>{1, 2, 3}; | |
System.assertEquals(numbers.keySet(), myNumbers); | |
//FIX ME! Populate the List called 'values' that you can then use to get the assert to pass | |
List<String> myValues = new List<String>{'Li', 'Er', 'San'}; | |
System.assertEquals(numbers.values(), myValues); | |
//FIX ME! On the Map Methods page in the Apex docs, find the method that allows you to remove an item from the Map | |
// and get the assertion to pass. | |
numbers.remove(1); | |
System.assertEquals(2, numbers.size()); | |
//FIX ME! And now find and use the method to remove all the values from the Map | |
// and get the assertion to pass. | |
numbers.clear(); | |
System.assert(numbers.isEmpty()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment