Collection – Exercises

1. Write the syntax for a List declaration.

2. Declare a list that can store integer values.
– Add the values 10,30,30,2,3,125 and get the size of the list.
– Remove the 2nd and 5th element of the list using list method.
– Get the 4th element of the list.
– Replace the last instance of ‘125’ with ‘100’
– Sort the list (which is allowed only for primitive types)

3. Write the syntax for a Set declaration.
4. What is set? When do we mostly use the Set?
5. Declare a set that can store integer values.
–   Add the values 10,30,30,2,3,125 and get the size of the set.
–  Remove the element 30 from the set using set method remove().
–  Add element 112 in the above set.
–  Clear the set using appropriate set method.
6. Write the syntax for a Map declaration.
7. What is Map? When do we mostly use the Map?

Sample Map:

Map m = new Map{1001 => 'Noel', 1004 => 'Ranjith', 1002 => 'Vidhya'};

Demonstrate the below ones
How to displays all keys ?
How to displays all values?
Does the key exist?
How to fetch the value for the key?
Add a new key/value pair
How to return the number of elements ?
How to remove an element ?
How to clone the map (refer the apex pdf under chapter – 4; collection for page 30 – 34
How to remove all elements ?
How to return true if zero elements ?