Created
April 28, 2025 15:08
-
-
Save abbiebauer37/8f9f8ea1a355626d8d3b9a8c5dec2079 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 WeekSixHomework { | |
// 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> colorsOfTheRainbow = new Set<String>(); | |
colorsOfTheRainbow.add('Red'); | |
colorsOfTheRainbow.add('Orange'); | |
colorsOfTheRainbow.add('Yellow'); | |
colorsOfTheRainbow.add('Green'); | |
colorsOfTheRainbow.add('Blue'); | |
colorsOfTheRainbow.add('Indigo'); | |
colorsOfTheRainbow.add('Violet'); | |
//Use System.debug to print out the size of your set | |
system.debug('Number of Colors in the Rainbow: ' + colorsOfTheRainbow.size()); | |
//Add an item to your set that already exists | |
colorsOfTheRainbow.add('Green'); | |
//Use System.debug to print out the size again, they should be the same! | |
system.debug('Number of Colors in the Rainbow: ' + colorsOfTheRainbow.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? | |
system.debug('The list of colors is empty: ' + colorsOfTheRainbow.isEmpty()); | |
} | |
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> myFruitsVeggies = new Map <String, String>(); | |
myFruitsVeggies.put ('Strawberry','Fruit'); | |
myFruitsVeggies.put ('Potato','Vegetable'); | |
myFruitsVeggies.put ('Watermelon','Fruit'); | |
myFruitsVeggies.put ('Kiwi','Fruit'); | |
myFruitsVeggies.put ('Broccoli','Vegetable'); | |
myFruitsVeggies.put ('Carrot','Vegetable'); | |
//Now, in the debug log, print out a list of the values. | |
system.debug('My map of fruits & veggies: ' + myFruitsVeggies); | |
//Can you print out a set of the Keys? | |
for (string key: myFruitsVeggies.keyset()){ | |
system.debug ('Item: ' + key); | |
} | |
// 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? | |
myFruitsVeggies.remove('Carrot'); | |
system.debug('My map of fruits & veggies: ' + myFruitsVeggies); | |
myFruitsVeggies.clear(); | |
system.debug('My map of fruits & veggies: ' + myFruitsVeggies); | |
} | |
public static void listsReview() { | |
// 1. Create a list of Strings. Add at least 5 entries to the list, all in one line | |
List<String> phoneticAlphabet = new List <String>{'Alpha','Bravo','Charlie','Delta','Echo','Foxtrot'}; | |
system.debug('My Phonetic Alphabet List: '+ phoneticAlphabet); | |
// 2. Create a second list of Strings and add 5 or more entries to this list. | |
List<String> monthsOfTheYear = new List <String>{'January','February','March','April','May','June','July','August','September','October','November','December'}; | |
system.debug('The months in a year: '+ monthsOfTheYear); | |
system.debug('Number of months in a year: '+ monthsOfTheYear.size()); | |
//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 | |
phoneticAlphabet.addAll(monthsOfTheYear); | |
system.debug('My Phonetic Alphabet List: '+ phoneticAlphabet); | |
system.debug('Number of Items in my List: ' + phoneticAlphabet.size()); | |
//4. Can you now loop through your combined list and print it to the debug log? (Hint: Check the documentation!) | |
for (String a : phoneticAlphabet){ | |
system.debug('My Combined List: ' + a); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment