Skip to content

Instantly share code, notes, and snippets.

@danicacodes
Created April 26, 2024 02:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danicacodes/b54c466f84d2f0992dd75edb865d3840 to your computer and use it in GitHub Desktop.
Save danicacodes/b54c466f84d2f0992dd75edb865d3840 to your computer and use it in GitHub Desktop.
Danica Weber HW6
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> stringSetLatin = new Set<String>();
//Add 5 entries to set
stringSetLatin.add('Paeonia');
stringSetLatin.add('Narcissus');
stringSetLatin.add('Helianthus annus');
stringSetLatin.add('Echinacea purpurea');
stringSetLatin.add('Rudbeckia hirta');
//Use System.debug to print out the size of your set
System.debug('Size of set1: ' + stringSetLatin.size());
//Add an item to your set that already exists
stringSetLatin.add('Viola tricolor');
//Use System.debug to print out the size again, they should be the same!
System.debug('Size of set2: ' + stringSetLatin.size());
//Second set of string
Set<String> stringSetLatin2 = new Set<String>{'Januarius', 'Februarius', 'Martius', 'Aprilis', 'Maius'};
System.debug('Size of set3: ' + stringSetLatin2.size());
stringSetLatin2.add('Junius');
System.debug('Size of set4: ' + stringSetLatin2.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> mySet = new Set<Integer>();
Boolean result = mySet.isEmpty();
System.assertEquals(true, 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,Integer> mapItemsQuantity = new Map<String,Integer>();
mapItemsQuantity.put('crackers',1);
mapItemsQuantity.put('butter',1);
mapItemsQuantity.put('sugar',4);
mapItemsQuantity.put('milk',1);
mapItemsQuantity.put('eggs',6);
//Now, in the debug log, print out a list of the values.
List<Integer> quantities = mapItemsQuantity.values();
System.debug('List of quantities:' + quantities);
//Can you print out a set of the Keys?
Set<String> keys = mapItemsQuantity.keySet();
System.debug('Set of keys: ' + keys);
// 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?
mapItemsQuantity.remove('sugar');
System.debug('Updated Map: ' + mapItemsQuantity);
mapItemsQuantity.clear();
System.debug('Cleared Map: ' + mapItemsQuantity);
}
public static void listsReview() {
// 1. Create a list of Strings. Add at least 5 entries to the list, all in one line
List<String> listOfPups = new List<String>{'Scottish Terrier', 'Great Dane', 'Welsh Terrier', 'Schnauzer', 'Westie'};
// 2. Create a second list of Strings and add 5 or more entries to this list.
List<String> listOfMorePups = new List<String>{'Retriever', 'Chihuahua', 'Yorkie', 'Maltese', 'Dachshund'};
//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
// Create a list based on an existing one
listOfMorePups.addAll(listOfPups);
//4. Can you now loop through your combined list and print it to the debug log? (Hint: Check the documentation!)
for(String breeds : listOfMorePups) {
System.debug(breeds);
}
}
}
@SalesforceKate
Copy link

  • On line 19, be sure to follow the instructions on line 18.
    Great job!

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