Skip to content

Instantly share code, notes, and snippets.

@JessWingerden
Created November 28, 2025 08:33
Show Gist options
  • Select an option

  • Save JessWingerden/ae870a92cd917dfbba8c04e25c2dc67c to your computer and use it in GitHub Desktop.

Select an option

Save JessWingerden/ae870a92cd917dfbba8c04e25c2dc67c to your computer and use it in GitHub Desktop.
Week 6 Homework
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
//JW - Creating my shopping list
Set<String> shoppingList = new Set<String>();
shoppingList.add('Sponges');
shoppingList.add('Dishwashing detegerent');
shoppingList.add('Milk');
shoppingList.add('Garlic');
shoppingList.add('Coffee');
//Use System.debug to print out the size of your set
System.debug('How many items are on my shoppingList:' + shoppingList.size());
//Add an item to your set that already exists
//JW: Added coffee because I was sleepy and realised I need coffee x2
shoppingList.add('Coffee');
//Use System.debug to print out the size again, they should be the same!
//JW: P.S it was still 5.
System.debug('How many items now I have added coffee twice:' + shoppingList.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?
//JW: Lets check if my list is empty.
Boolean result = shoppingList.isEmpty();
System.debug('Is my shopping list empty?: ' + result);
System.assertEquals(false, result);
//JW: Lets now clone the list so we can divide and conquer
Set<String> clonedShoppingList = shoppingList.clone();
System.debug('OG List:' + shoppingList);
System.debug('Cloned List' + clonedShoppingList);
}
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.
//Now, in the debug log, print out a list of the values.
Map<Integer, String> winePreferences = new Map<Integer, String>();
winePreferences.put(1, 'Rosé');
winePreferences.put(2, 'Riesling');
winePreferences.put(3, 'Sparkling');
winePreferences.put(4, 'Pinot Noir');
winePreferences.put(5, 'Shiraz');
//Can you print out a set of the Keys?
System.debug(winePreferences.keySet());
//JW:Practicing some loops to get individual values
for (String loopedwine : winePreferences.values()) {
System.debug('Listed Wine: ' + loopedwine);
}
//JW: Get the wine preference values
List<String> winePreferenceValues = winePreferences.values();
System.debug('Wine Preference values:' + winePreferenceValues);
// 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 updatedWinePreferences = winePreferences.remove(5);
}
public static void listsReview() {
// 1. Create a list of Strings. Add at least 5 entries to the list, all in one line
List<String> christmasFood = new List<String>{'Ham', 'Potatoes', 'Gravy', 'Salad', 'Turkey', 'Wine'};
System.debug('Im hungry and excited for Christmas' + christmasFood);
// 2. Create a second list of Strings and add 5 or more entries to this list.
List<String> typesOfWine = new List<String>();
typesOfWine.add('Rosé');
typesOfWine.add('Pinot Noir');
typesOfWine.add('Shiraz');
typesOfWine.add('Riesling');
typesOfWine.add('Sparkling');
System.debug('What types of wine will I be drinking?:' + typesOfWine);
//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
//JW: Lets add all the christmas goodies together
typesOfWine.addAll(christmasFood);
//4. Can you now loop through your combined list and print it to the debug log? (Hint: Check the documentation!)
// JW: Lets loop to create 1 list
for (string ListofItems : typesOfWine) {
System.debug('Christmas Goodies:' + ListofItems);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment