Iterate over the keys instead of the values:

for (Id id : mailContainer.keySet())
{
    System.debug(id);
    System.debug(mailContainer.get(id));
}
Answer from Adam Butler on Stack Overflow
🌐
JanBask Training
janbasktraining.com › community › salesforce › how-to-get-values-from-a-map-apex
How to get values from a map apex? | JanBask Training Community
September 23, 2022 - Answered by alex Duncan · You may use a Map apex for this: public static String manageFilters(Map lines){ System.debug('### lines : ' + lines); System.debug('### lines.values() : ' + lines.values()); for(String key : lines.keySet()){ System.debug('### lines.get(key) : ' + lines.get(key)); System.debug('### >>> ' + lines.get(key)); Map test = (Map)lines.get(key); System.debug('### test ' + test); System.debug('### test ' + test.get('ObjectName')); } return null; } Upvote 0 ·
Discussions

How to display Map's list value based on 1 key in Apex Salesforce? - Stack Overflow
Hello beautiful people, I have a scenario where I am defining a Map with string key and a list. Now, I want to put values in a list one by one and also display them using the single Key. Example co... More on stackoverflow.com
🌐 stackoverflow.com
Using Maps in Apex (beginner)
Sounds liek you don't have a grasp of what the differences are. These concepts are not unique to Apex. A list is an ordered sequence of items, like people waiting to get their number called. The thing is, one person can have more than one number assigned, so they can appear in the waiting list more than once. A set is almost like a list except it does not allow for a person to have more than one chance to be in the waiting list (so it only appears once on the whole waiting list) and it's not ordered, so doesn't matter what number the person got assigned to it. A map is a representation of the person waiting and it's number. That number is unique, so it's the key, the person assigned to the number is the value, so each entry of the map is a pair where you can't have two pairs with the same key. Where does it all make sense to use? Well, it depends on the use case. Let's say you want to check all the accounts of all the opportunities to get the account type, the account ID, the name and the opportunity Id and Opp name and export it to a csv so someone somewhere can analyze it. You do your SOQL query, get everything in a nice list and you output it into a nice excel sheet with a pretty header, mail it to the guy that requested it. You come up with this little piece of code: List lstOpp = [Select Id, AccountId, Account.Type from Opportunity]; String output = ''; for(Opportunity opp : lstOpp){ output+=opp.Id+','; output+=opp.AccountId+','; output+=opp.Account.Type+'\n'; } system.debug(output); A day later he replies: "hey! I'm noob at excel and there's duplicates here I don't know how to get rid of. Can you just send it to me without duplicates? Thanks!" You then think: Ok, I'll just add the Opportunities I get from the query and put it into a Set! That way, they get the information they want and no duplicates, right? Right!? Let's see... List lstopp = [Select Id, AccountId, Account.Type from Opportunity]; String output = ''; Set stOpp = new Set(); for(Opportunity opp : lstopp){ stOpp.add(opp); } for(Opportunity opp : stOpp){ output+=opp.Id+','; output+=opp.AccountId+','; output+=opp.Account.Type+'\n'; } system.debug(output); That't not right! My unique value is an opportunity... What I need is to have something that allows me to check if a certain Account is already added on the list I'm writting down on the CSV. Let's however see how this can be done by throwing a Map into the mix :) List lstopp = [Select Id, AccountId, Account.Type from Opportunity]; String output = ''; Map mapOppsByAccountId = new Map(); for(Opportunity opp : lstopp){ /* Notice what happens if you put the same key twice: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_map.htm#apex_methods_system_map */ mapOppsByAccountId.put(opp.AccountId,opp); } for(Id accountId : mapOppsByAccountId.keySet()){ Opportunity opp = mapOppsByAccountId.get(accountId); output+=opp.Id+','; output+=opp.AccountId+','; output+=opp.Account.Type+'\n'; } system.debug(output); Not the best example but I think it helps. If you have more questions (or if I made it even more complicated) let me know. More on reddit.com
🌐 r/salesforce
7
4
September 23, 2020
How do I get key from a value in map
Skip to main content · Register now for TDX! Join the must-attend event to experience what’s next and learn how to build it More on developer.salesforce.com
🌐 developer.salesforce.com
July 9, 2015
apex - How to check the map key value with field value? - Salesforce Stack Exchange
I am trying to write condition in the class if "field1__c is not equal to map1 key value and field2__c is not equal to map1 key value " then it should throw an error. After assigning when I check... More on salesforce.stackexchange.com
🌐 salesforce.stackexchange.com
🌐
Salesforce
trailhead.salesforce.com › trailblazer-community › feed › 0D54V00007T48SKSAZ
How do I get key from a value in map - Trailhead - Salesforce
Skip to main content · Register now for TDX! Join the must-attend event to experience what’s next and learn how to build it
🌐
Reddit
reddit.com › r/salesforce › using maps in apex (beginner)
r/salesforce on Reddit: Using Maps in Apex (beginner)
September 23, 2020 -

I’ve been learning Apex for a little bit now and have a pretty novice understanding of how it all works, but the concept I’m struggling with the most is using maps instead of lists or sets.

Does anyone have good reference material or examples on when Map is useful/what’s really happening in the background? I feel like I’m really close to fully grasping it, but there’s still something missing where I’m not fully understanding the value.

I keep watching tutorials online and they’re definitely inching me closer to a revelation but I’ve failed to find one that really digs in.

Top answer
1 of 5
10
Sounds liek you don't have a grasp of what the differences are. These concepts are not unique to Apex. A list is an ordered sequence of items, like people waiting to get their number called. The thing is, one person can have more than one number assigned, so they can appear in the waiting list more than once. A set is almost like a list except it does not allow for a person to have more than one chance to be in the waiting list (so it only appears once on the whole waiting list) and it's not ordered, so doesn't matter what number the person got assigned to it. A map is a representation of the person waiting and it's number. That number is unique, so it's the key, the person assigned to the number is the value, so each entry of the map is a pair where you can't have two pairs with the same key. Where does it all make sense to use? Well, it depends on the use case. Let's say you want to check all the accounts of all the opportunities to get the account type, the account ID, the name and the opportunity Id and Opp name and export it to a csv so someone somewhere can analyze it. You do your SOQL query, get everything in a nice list and you output it into a nice excel sheet with a pretty header, mail it to the guy that requested it. You come up with this little piece of code: List lstOpp = [Select Id, AccountId, Account.Type from Opportunity]; String output = ''; for(Opportunity opp : lstOpp){ output+=opp.Id+','; output+=opp.AccountId+','; output+=opp.Account.Type+'\n'; } system.debug(output); A day later he replies: "hey! I'm noob at excel and there's duplicates here I don't know how to get rid of. Can you just send it to me without duplicates? Thanks!" You then think: Ok, I'll just add the Opportunities I get from the query and put it into a Set! That way, they get the information they want and no duplicates, right? Right!? Let's see... List lstopp = [Select Id, AccountId, Account.Type from Opportunity]; String output = ''; Set stOpp = new Set(); for(Opportunity opp : lstopp){ stOpp.add(opp); } for(Opportunity opp : stOpp){ output+=opp.Id+','; output+=opp.AccountId+','; output+=opp.Account.Type+'\n'; } system.debug(output); That't not right! My unique value is an opportunity... What I need is to have something that allows me to check if a certain Account is already added on the list I'm writting down on the CSV. Let's however see how this can be done by throwing a Map into the mix :) List lstopp = [Select Id, AccountId, Account.Type from Opportunity]; String output = ''; Map mapOppsByAccountId = new Map(); for(Opportunity opp : lstopp){ /* Notice what happens if you put the same key twice: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_map.htm#apex_methods_system_map */ mapOppsByAccountId.put(opp.AccountId,opp); } for(Id accountId : mapOppsByAccountId.keySet()){ Opportunity opp = mapOppsByAccountId.get(accountId); output+=opp.Id+','; output+=opp.AccountId+','; output+=opp.Account.Type+'\n'; } system.debug(output); Not the best example but I think it helps. If you have more questions (or if I made it even more complicated) let me know.
2 of 5
3
Maps allow you to locate data in a list very easily. Let's say you needed to find a record based on Id. If you just used a list, you would have to iterate through the list using an if statement to find the record with the id that matched your id. With a Map you can simple do a Get statement to return the record with the matching Id. Thinking them as a way to arrange data for easy retrieval later.
🌐
Salesforce Developers
developer.salesforce.com › forums
How do I get key from a value in map
July 9, 2015 - Skip to main content · Register now for TDX! Join the must-attend event to experience what’s next and learn how to build it
Find elsewhere
🌐
SFDC Kid
sfdckid.com › 2019 › 04 › salesforce-apex-collection-map.html
APEX COLLECTION: Map - SFDC Kid
values() : It returns a list that contains all of the values in the map in arbitrary order. ... Set of key : key cannot be duplicate.
🌐
Salesforce Developers
developer.salesforce.com › docs › atlas.en-us.apexcode.meta › apexcode › langCon_apex_collections_maps.htm
Maps | Apex Developer Guide | Salesforce Developers
A map is a collection of key-value pairs where each unique key maps to a single value. Keys and values can be any data type—primitive types, collections, sObjects, user-defined types, and built-in Apex types.
🌐
Panther Schools
pantherschools.com › home › salesforce › how to use map collection in salesforce?
How to use Map Collection in Salesforce? » Panther Schools
April 21, 2025 - Map is very powerful while using Apex Code in Salesforce · Map<String, String> countryCurrencies = new Map<String, String>(); List<String> ABC = new List<String>(); #1 - Map<String, Integer> accountMap = new Map<String, Integer>(); accountMap.put('Acme', 10000); accountMap.put('XYZ Corp', 5000); accountMap.put('ABC Company', 7500); // Retrieve the value for a specific key Integer acmeValue = accountMap.get('Acme'); System.debug(acmeValue); // Outputs 10000 #2 - Map<String, Integer> accountMap = new Map<String, Integer>{ 'Acme' => 10000, 'XYZ Corp'=> 500 }; Boolean exists = accountMap.containsKey('Acme'); // exists = True accountMap.keySet(); // Returns the Set<Data Type of the Map Key> accountMap.values(); // Returns the List<Data Type of Map Value> Set<String> accountNameSet = accountMap.keySet(); List<Integer> revenueList = accountMap.values(); #3 - Map<String, Integer> accountMap
🌐
Mytutorialrack
mytutorialrack.com › home › understanding map methods in apex: key concepts and best practices
Understanding Map Methods in Apex: Key Concepts and Best Practices
June 1, 2023 - In Apex, the Map class provides a collection of key-value pairs, where each key is unique. It offers several methods to manipulate and retrieve data from the map. Map methods in Apex provide a wide range of functionality, including searching for values, checking for key existence, and merging maps together.
🌐
SaaS Guru
saasguru.co › salesforce-map-class
Salesforce Map Class: Comprehensive Guide
May 24, 2024 - Salesforce Map Class is a powerful tool that can be used to store and manage data in a key-value pair format. This guide will teach you everything you need to know.
🌐
JanbaskTraining
janbasktraining.com › home › salesforce maps: features, benefits & methods
Salesforce Maps: Features, Benefits & Methods
December 29, 2022 - Map Class keys and values can be of any data type—primitive types, collections, sObjects, user-defined types, and built-in Apex types. Map keys of type String are case-sensitive. Map methods, including put, get, containsKey, and remove treat these keys as distinct.
🌐
C# Corner
c-sharpcorner.com › article › apex-map-in-salesforce
Apex Map In SalesForce
September 19, 2019 - get(key) - using this method, we can get the value to which the key is mapped, or it generates null if the key has no value. ... Finally, the ApexMap.apxc class code is mentioned below.
🌐
Decodeforce
decodeforce.com › blogs › apex-map-class-methods-and-examples
Salesforce apex map class and methods with practice examples
Maps in Salesforce Apex are collections of key-value pairs. The keys are unique, and each key maps to a specific value.