Last active
September 16, 2020 20:41
-
-
Save radwomenunazhang/144c0484ed5663f8ff4fa57c74a9e03c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
public with sharing class WeekFiveHomework { | |
// Remember Sets & Maps?? We did a little practice with Lists in week 2, but we need to make sure we know how to use Sets & Maps as well!! | |
public static void setsReview(){ | |
//Your assignment: Play with Sets! | |
// 1. Create a set of Strings and add at least 5 entries | |
Set<String> SalesforceProducts = new Set<String>(); | |
SalesforceProducts.add('Sales Cloud'); | |
SalesforceProducts.add('Service Cloud'); | |
SalesforceProducts.add('Marketing Cloud'); | |
SalesforceProducts.add('Commerce Cloud'); | |
SalesforceProducts.add('Community Cloud'); | |
//Use System.debug to print out the size of your set | |
System.debug('Number of Salesforce Products ' + SalesforceProducts.size()); | |
//Add an item to your set that already exists | |
SalesforceProducts.add('Sales Cloud'); | |
//Use System.debug to print out the size again, they should be the same! | |
System.debug('Number of Salesforce Products ' + SalesforceProducts.size()); | |
// 2. Bonus Points! Check out the documentation on Sets. Go to the section titled Set Methods. Pick one of the methods to try out and print the results to the debug log. | |
// Documentation: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_set.htm | |
//Hint: can you print out a boolean that indicates if the set is empty? Can you clone it? | |
set<integer> newSet = new set<integer>(); | |
newSet.add(1); | |
system.debug( 'is the Set empty? ' +newSet.isempty()); | |
set<integer> cloneSet = new set<integer>(); | |
cloneSet = newSet.clone(); | |
system.debug( 'Can I clone it? ' +cloneSet); | |
} | |
public static void mapsReview () { | |
//Time to get creative! | |
// 1. Create a new Map. You can use whatever primitives/object types you like, as long as they make sense. | |
// Try to add at least 5 Key/value pairs. | |
map< string,string> newMap = new map<string, string>(); | |
newMap.put('blue', 'sky'); | |
newMap.put('green', 'tree'); | |
newMap.put('cute', 'cat'); | |
newMap.put('lovely', 'day'); | |
newMap.put('sunny', 'weather'); | |
//Now, in the debug log, print out a list of the values. | |
system.debug('5 key/value pair are ' +newMap.values()); | |
//Can you print out a set of the Keys? | |
system.debug(' the set of Keys are ' +newMap.keySet()); | |
// 2. Bonus! Check out the documentation on Maps. Go to the section titled Map Methods. Manipulate the Map using one of the methods defined in the documentation | |
// Documentation: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_map.htm | |
// Hint: Can you remove an entry using the key? Can you clear out the map without deleting it? | |
newMap.remove('cute'); | |
system.debug('key/value pair are ' +newMap); | |
newMap.clear(); | |
system.debug('key/value pair are ' +newMap); | |
} | |
public static void listsReview() { | |
// 1. Create a list of Strings. Add at least 5 entries to the list, all in one line | |
list<string> stringList = new list<string> { 'Blue','Red','Yellow','Black','White'}; | |
system.debug('The list of Strings contains'+stringList); | |
// 2. Create a second list of Strings and add 5 or more entries to this list. | |
list<string> secondStringList = new list<string> {'Amber','Grey','Turtoise','Purple','Pink'}; | |
system.debug('the second list of Strings contains '+secondStringList); | |
//3. Bonus! Using the documentation on standard List methods from Salesforce, add the items from your first list, to your second list with one command | |
// Documentation: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_list.htm | |
stringList.addAll(secondStringList); | |
system.debug('The combined list of Strings contains '+stringList); | |
//4. Can you now loop through your combined list and print it to the debug log? (Hint: Check the documentation!) | |
for(string s: stringList){ | |
system.debug('The combined list of Strings contains'+s); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you so much for correcting my homework Pat! I have made the updates accordingly.