Created
April 18, 2019 03:25
-
-
Save AngelOfCA/0288e5e6716365900a6e4457708aaf25 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> Colors = new Set<String>(); | |
Colors.add('Pink'); | |
Colors.add('Lime Green'); | |
Colors.add('Orange'); | |
Colors.add('Teal'); | |
Colors.add('Turquoise'); | |
//Use System.debug to print out the size of your set | |
System.debug('My Color List ' + Colors.size()); | |
//Add an item to your set that already exists | |
Colors.add('Turquoise'); | |
//Use System.debug to print out the size again, they should be the same! | |
System.debug('My Color List ' + Colors.size()); | |
// 2. Bonus Points! Check out the documentation on Sets. Go to the section titled List 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 if the set is empty? Can you clone it? | |
Set <Integer> NumberList = New Set<Integer> (); | |
NumberList.add(1); | |
NumberList.add(2); | |
NumberList.add(54); | |
NumberList.add(87); | |
NumberList.add(21); | |
NumberList.add(1234); | |
IF | |
(NumberList.contains (21)) { | |
System.debug('21 is in the list!'); | |
} else { | |
System.debug('21 is not in the list!'); | |
//comment out line 34 to print out that 21 is not in the list | |
} | |
NumberList.clone(); | |
NumberList.add(75); | |
System.debug('Cloned List' + NumberList); | |
NumberList.clear(); | |
system.debug('Clear My List' + NumberList); | |
NumberList.add(8765); | |
NumberList.add(94899); | |
NumberList.add(72183); | |
system.debug('New Number List ' + NumberList.size()); | |
} | |
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> NumberMonth = New Map <Integer, String> (); | |
NumberMonth.put(01, 'January'); | |
NumberMonth.put(02, 'February'); | |
NumberMonth.put(03, 'March'); | |
NumberMonth.put(04, 'April'); | |
NumberMonth.put(05, 'May'); | |
NumberMonth.put(06, 'June'); | |
NumberMonth.put(07, 'July'); | |
NumberMonth.put(08, 'August'); | |
NumberMonth.put(09, 'September'); | |
NumberMonth.put(10, 'October'); | |
NumberMonth.put(11, 'November'); | |
NumberMonth.put(12, 'December'); | |
//Now, in the debug log, print out a list of the values. | |
System.debug('Month list: ' + NumberMonth); | |
List<String> mapValuesList = numberMonth.values(); | |
System.debug('Months in my set: ' + mapValuesList); | |
//Can you print out a set of the Keys? | |
Set<Integer> mapKeysSet = numberMonth.keySet(); | |
System.debug('Key to my Map: ' + mapKeysSet); | |
// 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 mapKeysSet2 = numberMonth.remove(1); | |
System.debug('Remove ' + mapKeysSet2); | |
System.debug('New Map List ' + numberMonth); | |
//public Void clear(); | |
System.debug('Cleared Map List ' + numberMonth); | |
} | |
public static void listsReview() { | |
// 1. Create a list of Strings. Add at least 5 entries to the list, all in one line | |
List <String> petNames = new List <String> {'Fido', 'Oscar', 'Izzy','Rocky', 'Midnight', 'Roxy', 'Snowball'}; | |
// 2. Create a second list of Strings and add 5 or more entries to this list. | |
List <String> random = new List <String> {'What', 'Couch', 'Blanket', 'Box', 'Concert'}; | |
//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 | |
random.addAll(petNames); | |
//4. Can you now loop through your combined list and print it to the debug log? (Hint: Check the documentation!) | |
System.debug('Both List ' + random); | |
} | |
} |
Your map fu is strong!
On line 44 clone function is used for cloning a set to another set. So Set newset = currentset.clone(); would made more sense.
I didnt know you could use 01, 02 for Integer. Did this compile? If so wow :)
On line 110 it asks you to use loop (for or while) but still looks great! Good job!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Why not use a loop for #4 of ListsReviewed()?