-
-
Save radwomenunazhang/144c0484ed5663f8ff4fa57c74a9e03c to your computer and use it in GitHub Desktop.
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); | |
} | |
} |
Hi Una, take a look at line 21. The instruction says to add an item to the set that already exists in the set, but you added a new item. The reason to use a set instead of a list is that a set ignores (doesn't add) any duplicates. That's what this exercise is trying to show you -- that when you add a duplicate item, the list size does not increase.
Nice job on the bonus (line 29+)
At line 57, they want you to print out just the values from the map. For that, you need
newMap.values()
like you used in line 62.
Then at line 62, they're looking for the keys, so you neednewMap.keySet()
.For line 70, when you remove something from a map, you specify it just by the key, not including the value. So it would be
newMap.remove('cute')
Your work on the lists is all correct!
Thank you so much for correcting my homework Pat! I have made the updates accordingly.
Hi Una, take a look at line 21. The instruction says to add an item to the set that already exists in the set, but you added a new item. The reason to use a set instead of a list is that a set ignores (doesn't add) any duplicates. That's what this exercise is trying to show you -- that when you add a duplicate item, the list size does not increase.
Nice job on the bonus (line 29+)
At line 57, they want you to print out just the values from the map. For that, you need
newMap.values()
like you used in line 62.Then at line 62, they're looking for the keys, so you need
newMap.keySet()
.For line 70, when you remove something from a map, you specify it just by the key, not including the value. So it would be
newMap.remove('cute')
Your work on the lists is all correct!