0% found this document useful (0 votes)
9 views2 pages

Apex Code Examples for Map Operations

The document provides a series of tasks and code snippets related to the use of Maps in Apex programming. It covers creating, modifying, merging, and iterating over Maps, as well as handling specific scenarios involving student scores, fruit quantities, and related objects like Accounts and Contacts. Each task is designed to demonstrate different functionalities and best practices for working with Maps in Salesforce Apex.

Uploaded by

Vaish
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views2 pages

Apex Code Examples for Map Operations

The document provides a series of tasks and code snippets related to the use of Maps in Apex programming. It covers creating, modifying, merging, and iterating over Maps, as well as handling specific scenarios involving student scores, fruit quantities, and related objects like Accounts and Contacts. Each task is designed to demonstrate different functionalities and best practices for working with Maps in Salesforce Apex.

Uploaded by

Vaish
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1.

Write Apex code to create a Map<String, Integer> that stores student names and
their scores. Add 3 entries and retrieve score by Student name.

2. Write Apex code to declare a Map<String, Integer> and add three key-value pairs
representing fruit names and their quantities.

3. Write a code snippet to retrieve a value from a Map using a key and print it.

4. Write Apex code to check if a specific key exists in a Map, and print an appropriate
message.

5. Write code to iterate over a Map<String, String> of country codes and country
names, and print both key and value.

6. Write code to iterate over all key-value pairs in a Map<String, String> and print
them.

7. Write Apex code to remove a key-value pair from a Map, and print the size of the Map
before and after removal.

8. Write a code snippet demonstrating how to check if a key exists in a Map, and handle
the case where it doesn’t.

9. Write a method that converts a List<Account> into a Map<Id, Account>.

10. Given a Map of student names to scores (Map<String, Integer>), write code to
update the score for a particular student.

11. Write Apex code to merge two Map<Id, Contact> variables.

12. Given two Maps, Map<String, Integer> map1 and map2, write code to merge
map2 into map1.

13. Given a list of Contacts, write code to create a Map<Id, Contact> keyed by Contact
Id.

14. Write a method that accepts a List<Opportunity> and returns a Map<Id,


Opportunity> keyed by Opportunity Id.

15. Write Apex code to remove an entry from a Map and print the Map size before and after
removal.

16. Write a method that takes a List<Contact> and returns a Map<Id,


List<Contact>> grouping Contacts by their AccountId.

17. Write a trigger snippet where you first query Accounts and store them in a Map, then use
that Map to update related Contacts without SOQL inside loops.
18. Write Apex code demonstrating how to clone a Map and modify the clone without
affecting the original Map.

19. Write a method that sorts a Map by its values and returns a List of keys in sorted order.

20. Given two nested Maps Map<Id, Map<String, Decimal>>, write code to merge
them so that inner maps are merged correctly.

21. Write Apex code to group Contacts by their Account Id in a Map<Id,


List<Contact>>. Then, for a given Account Id, retrieve and print all related Contacts.

22. Write a trigger snippet where you get all Opportunities related to a list of Accounts and
store them in a Map<Id, List<Opportunity>>. Then update a custom field on
Accounts based on the number of related Opportunities.

23. Given unrelated custom objects Invoice__c and Payment__c that have no direct
relationship, write Apex code to create two separate Maps keyed by their respective Ids,
then match them on a common external field (e.g., Invoice_Number__c) to print
matched pairs.

24. Write code to merge data from related objects: Query Accounts and their Contacts, then
build a Map<Id, AccountWrapper> where AccountWrapper is a class holding the
Account and a List of related Contacts.

25. Write Apex code to query all Accounts and store them in a Map<Id, Account>. Then,
query Contacts and print each Contact’s Account Name using the Map.

26. Write a method that takes a list of Opportunities and returns a Map<Id, Account>
where the key is [Link] and the value is the related Account
record. (Hint: Query Accounts based on Opportunity AccountIds).

27. Given two unrelated objects — Account and Case — write Apex code to store all
Accounts in a Map by Id, and then loop through Cases and print Account names if
[Link] matches.

You might also like