First, Maps are defined with two data types, such that we would express this in generic terms written with a unique letter for each data type involved: Map<T, U>, or as the documentation states: Map<T1, T2>. I prefer the classic nomenclature, but choose whichever you feel you understand better.

In your case, T is Id, and U is Map<T′, U′>. T′ and U′ are the type String. If you remember that, it should be easier to construct complicated types in the future:

Map< T ,            U          >
Map< Id, Map<   T′  ,   U′   > >
Map< Id, Map< String, String > > 

Second, to populate nested structures, you have to put them inside each other. In other words, before you can populate a value in the inner map, the outer map must have the object created first. To create most objects, we use the new keyword. One new is one new object.

When we create a new object, we call a constructor, which can take zero or more parameters to configure the initial state of the object. They are normal methods, which means we need to pass in any parameters, like we would any other method. new Map<Id, Map<String, String>>(), for example, constructs a new object with the compound data type Map<Id, Map<String, String>>.

So, to populate such a Map, we would do something like:

Map<Id, Map<String, String>> myMap = new Map<Id, Map<String, String>>();
myMap.put(obj.Id, new Map<String, String>());
myMap.get(obj.Id).put('value1','value2');

Note that put displaces the previous value if it exists, so we normally check first:

Map<String, String> myMapValue = myMap.get(obj.Id);
if(myMapValue == null) {
  myMapValue = new Map<String, String>();
  myMap.put(obj.Id, myMapValue);
}
myMapValue.put('value1','value2');

You can initialize a Map with the {} constructor format:

Map<Id, Map<String, String>> myMap = new Map<Id, Map<String, String>> {
  obj.Id => new Map<String, String> {
    'value1' => 'value2'
  }
};

Notice how there are two new keywords. We are still creating new objects, just placing one inside the other initially. The => operator simply means "key => value." This is the only use of => in Apex at this time.

Note that putAll is used to put a bunch of values inside the map at once. It requires that the parameter provided is the same type as U from our example above.

Map<Id, Map<String, String>> myMap = new Map<Id, Map<String, String>>();
Map<String, String> params = new Map<String, String>();
params.put('value1','value2');
myMap.put(obj.Id, new Map<String, String>());
myMap.get(obj.Id).putAll(params);
Answer from sfdcfox on Stack Exchange
Top answer
1 of 2
5

First, Maps are defined with two data types, such that we would express this in generic terms written with a unique letter for each data type involved: Map<T, U>, or as the documentation states: Map<T1, T2>. I prefer the classic nomenclature, but choose whichever you feel you understand better.

In your case, T is Id, and U is Map<T′, U′>. T′ and U′ are the type String. If you remember that, it should be easier to construct complicated types in the future:

Map< T ,            U          >
Map< Id, Map<   T′  ,   U′   > >
Map< Id, Map< String, String > > 

Second, to populate nested structures, you have to put them inside each other. In other words, before you can populate a value in the inner map, the outer map must have the object created first. To create most objects, we use the new keyword. One new is one new object.

When we create a new object, we call a constructor, which can take zero or more parameters to configure the initial state of the object. They are normal methods, which means we need to pass in any parameters, like we would any other method. new Map<Id, Map<String, String>>(), for example, constructs a new object with the compound data type Map<Id, Map<String, String>>.

So, to populate such a Map, we would do something like:

Map<Id, Map<String, String>> myMap = new Map<Id, Map<String, String>>();
myMap.put(obj.Id, new Map<String, String>());
myMap.get(obj.Id).put('value1','value2');

Note that put displaces the previous value if it exists, so we normally check first:

Map<String, String> myMapValue = myMap.get(obj.Id);
if(myMapValue == null) {
  myMapValue = new Map<String, String>();
  myMap.put(obj.Id, myMapValue);
}
myMapValue.put('value1','value2');

You can initialize a Map with the {} constructor format:

Map<Id, Map<String, String>> myMap = new Map<Id, Map<String, String>> {
  obj.Id => new Map<String, String> {
    'value1' => 'value2'
  }
};

Notice how there are two new keywords. We are still creating new objects, just placing one inside the other initially. The => operator simply means "key => value." This is the only use of => in Apex at this time.

Note that putAll is used to put a bunch of values inside the map at once. It requires that the parameter provided is the same type as U from our example above.

Map<Id, Map<String, String>> myMap = new Map<Id, Map<String, String>>();
Map<String, String> params = new Map<String, String>();
params.put('value1','value2');
myMap.put(obj.Id, new Map<String, String>());
myMap.get(obj.Id).putAll(params);
2 of 2
1

Try this

    Map<Id,Map<string,string> myMap = new Map<Id,Map<string,string>{}
myMap.put(obj.Id, new map<string, string>{'test' => 'test'});

More details here : https://stackoverflow.com/questions/9521740/apex-map-within-a-map

Discussions

triggers - Apex Map within a Map - Stack Overflow
I need to have a Map within a Map in one of my Triggers. I know how to build the map initially as that is documented: Map More on stackoverflow.com
🌐 stackoverflow.com
Using Maps in Apex (beginner)
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… More on reddit.com
🌐 r/salesforce
7
4
September 23, 2020
apex - I have a Map<String, Account> I want to insert the account in Salesforce and Create a new Map<String,accId> - Stack Overflow
Thank you very much for helping. Problem Statement I have a Map containing Map a string key and Salesforce Account, I want to insert the Account but for each account, I want to create a new Map w... More on stackoverflow.com
🌐 stackoverflow.com
apex - How to add value in map of map? - Salesforce Stack Exchange
I am trying to add map of map values, I have to intialize the map key first and then try to add value later. Below is the exact logic i am trying to achieve. Map More on salesforce.stackexchange.com
🌐 salesforce.stackexchange.com
🌐
Reddit
reddit.com › r/salesforce › using maps in apex (beginner)
r/salesforce on Reddit: Using Maps in Apex (beginner)
September 23, 2020 - SObjects allow us to use get and put methods like maps customContact.put(customField, con.get(field)); customContacts.add( customContact); } } insert customContacts; Using maps can open the door to a performant org storing query results and sharing those values through different apps using maps. ... For the first time since launch, Apex has officially gone 2 years without a new map.
🌐
CRS Info Solutions
crsinfosolutions.com › home › what is a map class in salesforce apex? with examples
What is a Map class in Salesforce Apex? with examples
Salesforce Training - The keys in a Map must be unique and are used to access the corresponding values. Apex provides several methods to manipulate these entries, such as put(), get(), and remove(), offering developers powerful tools to manage data effectively within their applications. I have enrolled for Salesforce Admin and development online course at CRS info solutions. It’s really the best training i have ever taken and syllabus is highly professional
Rating: 5 ​
🌐
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.
Find elsewhere
🌐
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 ·
🌐
SFDC Kid
sfdckid.com › 2019 › 04 › salesforce-apex-collection-map.html
APEX COLLECTION: Map - SFDC Kid
Now unlike list and set, there are attributes for Map as well. Following are the attributes of apex collection Map : clear() : It removes all of the key-value mappings from the map. clone() : It makes a duplicate copy of the map. containskey(key) : It will return true if the map contains a mapping for the specified key.
🌐
Iterative Logic
iterativelogic.com › working-with-apex-code-maps
Working with Apex Code Maps - Iterative Logic
May 10, 2015 - Once you have instantiated a map, you can add values to the map simply by using the put() method. However, if you happen to have a list of sObjects you can just pass that list in the constructor like so: Map<Id, Account> accountsById = new Map<Id, ...
🌐
S2 Labs
s2-labs.com › home › map initialization methods in apex
Map Initialization Methods In Apex | Salesforce Developer Tutorials
April 27, 2026 - In Salesforce Apex, Maps are collections of key-value pairs, where each unique key maps to a single value. Initializing Maps in Apex can be done in various ways depending on the requirements.
🌐
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 - In Apex, you can get values from a map using the get() method or by directly accessing the map keys. Here are examples of both approaches: Using the get() method:Map myMap = new Map(); myMap.put('key1', 10); myMap.put('key2', 20); Integer value1 ...
🌐
YouTube
youtube.com › force fellow
Map in Apex | Apex Fundamentals | Salesforce Developer - YouTube
This video is the part of Apex Fundamentals series, which covers map collection in apex. And in this video we have discussed about map defination and importa...
Published: October 9, 2023
Views: 752
🌐
Forceshark
forceshark.com › blog › key-concepts-and-usage-of-maps-in-salesforce-apex
Key concepts and usage of Maps in Salesforce Apex | Blog
You can add key-value pairs to the map using the put method: ... for (String name : myMap.keySet()) { Integer age = myMap.get(name); System.debug(name + ' is ' + age + ' years old.'); } Maps allow flexibility in data types for keys and values. You can use various data types based on your specific requirements. Internally, many programming languages, including Salesforce Apex, implement maps using hash tables.
🌐
YouTube
youtube.com › watch
How to initialize Maps in Apex? | Salesforce Development Course for Beginners - YouTube
In this video Shrey Covered answers to the following questions:How to create and initialize maps?How to initialize and assign Values together?What are the me...
Published: June 28, 2021