Last active
September 26, 2019 20:43
-
-
Save katelessardrad/422b9e008e8b050177dd4e495ec017d7 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
public with sharing class WeekFiveHomework { | |
public static void setsReview(){ | |
//Your assignment: Play with Sets! | |
// 1. Create a set of Strings and add at least 5 entries | |
Set <String> pizzaToppings = new Set <String>(); | |
pizzaToppings.add ('sauce'); | |
pizzaToppings.add('cheese'); | |
pizzaToppings.add('pepperoni'); | |
pizzaToppings.add('mushrooms'); | |
pizzaToppings.add('pineapple'); | |
//Use System.debug to print out the size of your set | |
system.debug('Number of pizza toppings: ' + pizzaToppings.size()); | |
//Add an item to your set that already exists | |
pizzaToppings.add('cheese'); | |
//Use System.debug to print out the size again, they should be the same! | |
system.debug('Number of pizza toppings: '+ pizzaToppings.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? | |
Boolean result = pizzaToppings.isEmpty(); | |
System.assertEquals(false,result); | |
System.debug('There are no pizza toppings in this set: ' + result); | |
} | |
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> friendLocations = new Map <String,String>(); | |
friendLocations.put ('Lisa','Seattle'); | |
friendLocations.put ('Caitlyn','Portland'); | |
friendLocations.put ('Sarah', 'Conifer'); | |
friendLocations.put ('Sharon', 'Denver'); | |
friendLocations.put ('Kimmy','Milwaukee'); | |
//Now, in the debug log, print out a list of the values. | |
List <String> cityValues = friendLocations.values(); | |
System.debug('I have friends that live in: ' + cityValues); | |
//Can you print out a set of the Keys? | |
Set <String> nameKeys = friendLocations.keyset(); | |
System.debug('My friends are named: ' + nameKeys); | |
// 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? | |
String friend1 = friendLocations.remove ('Lisa'); | |
System.debug('The four remaining friends are: ' + nameKeys); | |
friendLocations.clear(); | |
System.debug('I cleared out the map... see: ' + nameKeys); | |
} | |
public static void listsReview() { | |
// 1. Create a list of Strings. Add at least 5 entries to the list, all in one line | |
List<String> vacationBucketList = new List<String> {'Toscana','Cape Town','Munich','Christ Church','Kyoto'}; | |
// 2. Create a second list of Strings and add 5 or more entries to this list. | |
List<String> stayActive = new List<String> {'yoga','run','dance','pilates','barre'}; | |
//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 | |
vacationBucketList.addAll(stayActive); | |
List<String> combinedList = vacationBucketList; | |
System.debug('My new list: '+ combinedList); | |
//4. Can you now loop through your combined list and print it to the debug log? (Hint: Check the documentation!) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Swati- this week was awesome and really clicked. Everything came back how I wanted in the debug log (yay)!
I struggled with the final question... looping through the combined list and printing to the debug log. Can we take a look in class on Monday maybe?