Skip to content

Instantly share code, notes, and snippets.

@asmitakhare
Last active September 14, 2020 22:57
Show Gist options
  • Save asmitakhare/6f629e19b0972a46836f2ae173a4743b to your computer and use it in GitHub Desktop.
Save asmitakhare/6f629e19b0972a46836f2ae173a4743b to your computer and use it in GitHub Desktop.
Asmita Week 5 Homework.apxc
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> firstStringSet = new Set<String>();
firstStringSet.add('Sub');
firstStringSet.add('Burger');
firstStringSet.add('Sandwich');
firstStringSet.add('Fries');
firstStringSet.add('Ice-cream');
//Use System.debug to print out the size of your set
System.debug('The size of the s before adding Sub : ' + firstStringSet.size());
//Add an item to your set that already exists
firstStringSet.add('Sub');
//Use System.debug to print out the size again, they should be the same!
System.debug('The size of the s after adding Sub : ' + firstStringSet.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> numbersCountingBy20 = new Set<Integer> {20, 40,60,80,100};
System.debug('numbersCountingBy20 : ' + numbersCountingBy20);
System.debug('is numbersCountingBy20 Empty? : ' + numbersCountingBy20.isEmpty());
Set<Integer> clonedNumbersCountingBy20 = numbersCountingBy20.clone();
System.debug('clonedSet : ' + clonedNumbersCountingBy20);
}
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> myFirstMap = new Map<Integer, String>{10 => 'Reine', 14 => 'Dee', 13 => 'Jay', 10 => 'Anne', 12 => 'Bill'};
//Now, in the debug log, print out a list of the values(Reine, Dee, Jay, Anne, Bill)
//https://blog.jeffdouglas.com/2011/01/06/fun-with-salesforce-collections/
System.debug('myFirstMap variable values :' + myFirstMap.values());
//Can you print out a set of the Keys? Yes, can printout a set of the keys (10, 14, 13, 10, 12)
//https://blog.jeffdouglas.com/2011/01/06/fun-with-salesforce-collections/
System.debug('myFirstMap variable keys :' + myFirstMap.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('Map before deleting 13:' + myFirstMap);
myFirstMap.remove(13);
System.debug('Map after deleting 13:' + myFirstMap);
myFirstMap.clear();
System.debug('is myFirstMap empty aftger clear? ' + myFirstMap.isEmpty());
}
public static void listsReview() {
// 1. Create a list of Strings. Add at least 5 entries to the list, all in one line
List<String> stateList1 = new List<String>{'CA', 'FL', 'MA', 'NY', 'CO'};
// 2. Create a second list of Strings and add 5 or more entries to this list.
List<String> stateList2 = new List<String>();
stateList2.add('AR');
stateList2.add('IL');
stateList2.add('KS');
stateList2.add('ID');
stateList2.add('ME');
//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
List<String> myListOfString = new List<String>();
myListOfString.addAll(stateList1);
myListOfString.addAll(stateList2);
//4. Can you now loop through your combined list and print it to the debug log? (Hint: Check the documentation!)
for(String starString : myListOfString){
System.debug('combined list element : ' + starString);
}
}
//refered from David Liu videos
public static void test(){
listsReview();
mapsReview();
setsReview();
}
}
@chrisyojay
Copy link

ln53-- cool callout! I sometimes forget you can print out things without iterating through them, so I did more work and looped through to print the values out. Without looking it up, if someMap.keySet() returns a set, what does someMap.values() return?

@chrisyojay
Copy link

ln 48-- didn't even know you could do that

@chrisyojay
Copy link

ln 104-- clever way to reduce the amount you had to type for the execute anonymous

@chrisyojay
Copy link

chrisyojay commented Sep 14, 2020

Asmita-- stellar job! My only callouts: please use a variable name more descriptive than 's' and capitalize System for System.debug(...). Reason being is variables are camelCase, methods are camelCase, while classes are PascalCase-- System is just a class that someone else wrote that we utilize, so it's easier and cleaner to correct convention. These conventions I just mentioned are Apex-specific.

@asmitakhare
Copy link
Author

ln 48-- didn't even know you could do that

Thanks Christine for the comments! I learned those from the Trailhead and pre-reading links.

@asmitakhare
Copy link
Author

ln 104-- clever way to reduce the amount you had to type for the execute anonymous

I learned from David Liu.

@asmitakhare
Copy link
Author

Asmita-- stellar job! My only callouts: please use a variable name more descriptive than 's' and capitalize System for System.debug(...). Reason being is variables are camelCase, methods are camelCase, while classes are PascalCase-- System is just a class that someone else wrote that we utilize, so it's easier and cleaner to correct convention. These conventions I just mentioned are Apex-specific.

Thank you for the encouragement.

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