Skip to content

Instantly share code, notes, and snippets.

@olgaloza
Created March 19, 2018 03:19
Show Gist options
  • Save olgaloza/1aa6429ee974c76ae23f9c745b50c488 to your computer and use it in GitHub Desktop.
Save olgaloza/1aa6429ee974c76ae23f9c745b50c488 to your computer and use it in GitHub Desktop.
HomeworkFour()
public with sharing class WeekFourHomework {
public static void setsReview(){
//Your assignment: Play with Sets!
// 1. Create a set of Strings and add at least 5 entries
Set<String> names = new Set<String>();
names.add('Olga');
names.add('Tania');
names.add('Katia');
names.add('Polina');
names.add('Masha');
//Use System.debug to print out the size of your set
System.debug('Size of my name set is ' + names.size());
//Add an item to your set that already exists
names.add('Tania');
//Use System.debug to print out the size again, they should be the same!
System.debug('Size of my name set is ' + names.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<String> moreNames = names.clone();
System.debug('New set of names: ' + moreNames);
moreNames.clear();
System.debug('Can you print out moreNames if the set is empty? ' + (moreNames.size() > 0 ? 'yes': 'no'));
}
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> ageGroups = new Map<Integer, String>();
ageGroups.put(1, '0-5');
ageGroups.put(2, '6-10');
ageGroups.put(3, '11-15');
ageGroups.put(4, '16-20');
ageGroups.put(5, '21+');
//Now, in the debug log, print out a list of the values.
System.debug('Values of \'ageGroups\' are: ' + ageGroups.values());
//Can you print out a set of the Keys?
System.debug('And the Keys of \'ageGroups\' are: ' + ageGroups.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?
ageGroups.remove(1);
System.debug('Values now are: ' + ageGroups.values());
ageGroups.clear();
System.debug('Size of \'ageGroups\' is: ' + ageGroups.size());
System.debug('Does the \'ageGroups\' still exist? ' + ageGroups.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> loveNames = new List<String>{'Babe', 'Schmoopy', 'Peaches', 'Sugarbuns', 'Pumpkin'};
// 2. Create a second list of Strings and add 5 or more entries to this list.
List<String> desserts = new List<String>{'Honey', 'Cupcake', 'Cookie', 'Candy', 'Sugar pie'};
//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
loveNames.addAll(desserts);
System.debug('Size of the loveNames is ' + loveNames.size());
//4. Can you now loop through your combined list and print it to the debug log? (Hint: Check the documentation!)
System.debug('Iterate:');
for (String x : loveNames) {
System.debug(x);
}
}
}
@lmeerkatz
Copy link

@olgaloza Nice use of a ternary operator!

System.debug('Can you print out moreNames if the set is empty? ' + (moreNames.size() > 0 ? 'yes': 'no'));

Another option for checking whether a set is empty is to use the isEmpty() method:

(moreNames.size() > 0 ? 'yes': 'no') can also be written as (!moreNames.isEmpty() ? 'yes': 'no')

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