function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
RAG_SFDCRAG_SFDC 

Adding values into Nested maps

Hi Guys,

 

I am trying to add values into nested map

 

here it is

 

Map<String,Map<String,Map<String,List<Case>>>> xyz Map = new Map<String,Map<String,Map<String,List<Case>>>>();

 

 

How to add values to this map.

 

Appreciate your help :)

 

Best Answer chosen by Admin (Salesforce Developers) 
Sean TanSean Tan

Before anything else you probably want to rethink why you need such a nested set of maps. An Internal class wrapper object is probably better and a lot easier to manage.

 

In any case, if you really want to keep using the nested map structure... you can do something like this:

 

Map<String,Map<String,Map<String, Case[]>>> xyzMap = new Map<String,Map<String,Map<String, Case[]>>>();
addItemToMap(xyzMap, 'firstkey', 'secondkey', 'thirdkey', new Case(Subject='Test'));

private static void addItemToMap(Map<String,Map<String,Map<String, Case[]>>> nestedMap, String firstKey, String secondKey, String thirdKey, Case c)
{
	Map<String, Map<String, Case[]>> firstLevel = nestedMap.get(firstKey);

	if (firstLevel == null)
	{
		firstLevel = new Map<String, Map<String, Case[]>>{};
		nestedMap.put(firstKey, firstLevel);
	}

	Map<String, Case[]> caseMap = firstLevel.get(secondKey);

	if (caseMap == null)
	{
		caseMap = new Map<String,Case[]>{};
		firstLevel.put(secondKey, caseMap);
	}

	Case[] cList = caseMap.get(thirdKey);

	if (cList == null)
	{
		cList = new Case[]{};
		caseMap.put(thirdKey, cList);
	}

	cList.add(c);
}

 

All Answers

Sean TanSean Tan

Before anything else you probably want to rethink why you need such a nested set of maps. An Internal class wrapper object is probably better and a lot easier to manage.

 

In any case, if you really want to keep using the nested map structure... you can do something like this:

 

Map<String,Map<String,Map<String, Case[]>>> xyzMap = new Map<String,Map<String,Map<String, Case[]>>>();
addItemToMap(xyzMap, 'firstkey', 'secondkey', 'thirdkey', new Case(Subject='Test'));

private static void addItemToMap(Map<String,Map<String,Map<String, Case[]>>> nestedMap, String firstKey, String secondKey, String thirdKey, Case c)
{
	Map<String, Map<String, Case[]>> firstLevel = nestedMap.get(firstKey);

	if (firstLevel == null)
	{
		firstLevel = new Map<String, Map<String, Case[]>>{};
		nestedMap.put(firstKey, firstLevel);
	}

	Map<String, Case[]> caseMap = firstLevel.get(secondKey);

	if (caseMap == null)
	{
		caseMap = new Map<String,Case[]>{};
		firstLevel.put(secondKey, caseMap);
	}

	Case[] cList = caseMap.get(thirdKey);

	if (cList == null)
	{
		cList = new Case[]{};
		caseMap.put(thirdKey, cList);
	}

	cList.add(c);
}

 

This was selected as the best answer
sandeep@Salesforcesandeep@Salesforce

here is very simple example to get idea of putting values in nested Map: 

List<String> SourceList = new List<String>{'TestData1','TestData2'};
List<String> innerKeys = new List<String>{'innerOne','innerTwo'};
List<String> values = new List<String>{'valueOne','valueTwo'};

Map<String,Map<String,String>> NestedMAp = new Map<String,Map<String,String>>();
Map<String,String> interMap
for(Integer i = 0; i< SourceList.size(); i++){

interMap = new Map<String,String>();

for(Integer j = 0; j<innerKeys.size(); j++){
interMap.put(innerKeys[j],values[j]);
}

NestedMAp.put(SourceList[i],interMap);

}

RAG_SFDCRAG_SFDC

Perfect :)

 

Thanks Guys