Skip to content

Instantly share code, notes, and snippets.

@niavesper
Created March 16, 2020 02:54
Show Gist options
  • Save niavesper/dfd7ec135c988039e26ec7bb6c365e2a to your computer and use it in GitHub Desktop.
Save niavesper/dfd7ec135c988039e26ec7bb6c365e2a 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> breadTypes = new Set <String>{'Baguette', 'Lavash', 'Naan', 'Challah', 'Pita'};
//Use System.debug to print out the size of your set
System.debug(breadTypes.size());
//Add an item to your set that already exists
breadTypes.add('Baguette');
//Use System.debug to print out the size again, they should be the same!
System.debug(breadTypes.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
breadTypes.remove('Baguette');
System.debug(breadTypes);
String[] moreBread = new String[]{'Bagel','Focaccia','Brioche'};
breadTypes.addAll(moreBread);
System.debug(breadTypes);
//Hint: can you print out a boolean that indicates if the set is empty? Can you clone it?
Set <String> breadTypesNew = breadTypes.clone();
System.debug(breadTypesNew);
breadTypesNew.clear();
System.debug('The new list is empty now. True or false? ' + breadTypesNew.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 <Integer, String> artwork = new Map <Integer, String> {1 => 'Mona Lisa', 2 => 'The Birth of Venus', 3 => 'Pieta', 4 => 'The Back Cube', 5 => 'The Thinker'};
//Now, in the debug log, print out a list of the values.
System.debug(artwork.values());
//Can you print out a set of the Keys?
System.debug(artwork.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?
System.debug(artwork.get(2));
artwork.remove(4);
System.debug(artwork);
artwork.clear();
System.debug(artwork);
}
public static void listsReview() {
// 1. Create a list of Strings. Add at least 5 entries to the list, all in one line
List<String> australianAnimals = new List<String>{'Koala', 'Kangaroo', 'Bandicoot', 'Wombat', 'Dingo'};
// 2. Create a second list of Strings and add 5 or more entries to this list.
List<String> australianAnimals2 = new List<String>{'Kookaburra', 'Cockatoo', 'Wallabi', 'Emu', 'Quokka'};
//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
australianAnimals.AddAll(australianAnimals2);
System.debug(australianAnimals);
//4. Can you now loop through your combined list and print it to the debug log? (Hint: Check the documentation!)
for (String aA : australianAnimals){
System.debug(aA);}
}
}
@dharmarajan-krithika
Copy link

Beautiful..Loved that you have attempted the Bonus for all 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment