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
Vijay@sfdcVijay@sfdc 

Map<String,Map<String,List<String>>> mapOfMaps = new Map<String,Map<String,List<String>>>();

Hello everyone,
 can any one explain how to play with bellow map , how its usful in developement . can you provide proper example with below map.

Map<String,Map<String,List<String>>> mapOfMaps = new Map<String,Map<String,List<String>>>();

Thanks to all  advance :)
Best Answer chosen by Vijay@sfdc
AnupPrakash_AlgoworksAnupPrakash_Algoworks
List<City> cities = new List<City>();
	
	cities.add(new City('Mesa','Arizona','US'));
	cities.add(new City('Phoenix','Arizona','US'));
	cities.add(new City('Miami','Florida','US'));
	cities.add(new City('Patna','Bihar','IN'));
	cities.add(new City('Samastipur','Bihar','IN'));
	cities.add(new City('Brisbane','Queensland','AU'));
	cities.add(new City('Caboolture','Queensland','AU'));
	cities.add(new City('Airdrie','Alberta','CA'));
	cities.add(new City('Camrose','Alberta','CA'));
		
	Map<String,Map<String,List<String>>> mapOfMaps = new Map<String,Map<String,List<String>>>();
	
	
	
	
	
	for(City  cityObj : Cities){
	/*
		Logic could be implemented better 
	*/
		if(mapOfMaps.containsKey(cityObj.country)){
			mapOfMaps.get(cityObj.state).add(cityObj.cityName);
		}
		else{
			List<String> cities = new List<String>();
			cities.add(cityObj.cityName);
			Map<String,List<String>> stateToCities = new Map<String,List<String>>();
			stateToCities.put(cityObj.state,stateToCities);
			mapOfMaps.put(cityObj.country,stateToCities);
		}
	}

	
	public Class City{
	
		String cityName{ get; set; }
		String state{ get; set; }
		String country{ get; set; }
		
		publci city(String cityName, String state, String country){
			this.cityName = cityName;
			this.state = state;
			this.country = country;
		}
		
	}

Please mark as best answer if it solves your query

All Answers

Vasani ParthVasani Parth
Hi Vijay,

Here you go :
Map<String,Map<String,String>>initStringStringMap = new Map<String,Map<String,String>>{'one' => new Map<String, String>{'a'=>'b', 'c'=> 'd'}, 'two' =>new Map<String, String> {'e'=>'f', 'g'=> 'h'} };
for(String outerKey : initStringStringMap.keySet())
for(String innerKey : initStringStringMap.get(outerKey).keySet())
System.debug(' -----------  >>>>>>>>>>>>>>>' + initStringStringMap.get(outerKey).get(innerKey));

Please mark this as the best answer if this helps
AnupPrakash_AlgoworksAnupPrakash_Algoworks
List<City> cities = new List<City>();
	
	cities.add(new City('Mesa','Arizona','US'));
	cities.add(new City('Phoenix','Arizona','US'));
	cities.add(new City('Miami','Florida','US'));
	cities.add(new City('Patna','Bihar','IN'));
	cities.add(new City('Samastipur','Bihar','IN'));
	cities.add(new City('Brisbane','Queensland','AU'));
	cities.add(new City('Caboolture','Queensland','AU'));
	cities.add(new City('Airdrie','Alberta','CA'));
	cities.add(new City('Camrose','Alberta','CA'));
		
	Map<String,Map<String,List<String>>> mapOfMaps = new Map<String,Map<String,List<String>>>();
	
	
	
	
	
	for(City  cityObj : Cities){
	/*
		Logic could be implemented better 
	*/
		if(mapOfMaps.containsKey(cityObj.country)){
			mapOfMaps.get(cityObj.state).add(cityObj.cityName);
		}
		else{
			List<String> cities = new List<String>();
			cities.add(cityObj.cityName);
			Map<String,List<String>> stateToCities = new Map<String,List<String>>();
			stateToCities.put(cityObj.state,stateToCities);
			mapOfMaps.put(cityObj.country,stateToCities);
		}
	}

	
	public Class City{
	
		String cityName{ get; set; }
		String state{ get; set; }
		String country{ get; set; }
		
		publci city(String cityName, String state, String country){
			this.cityName = cityName;
			this.state = state;
			this.country = country;
		}
		
	}

Please mark as best answer if it solves your query
This was selected as the best answer
Vijay@sfdcVijay@sfdc
Thanks Vasani Parth for reply.
Hello Anup,

 
could you please explain the below code, i have seen it in my org code also, why do we use the below lines. even we dont have any data.
i worte testclasses. that time these lines always failed. i am really big head breaking with bellow line of code and use of it.
if(mapOfMaps.containsKey(cityObj.country)){ 
mapOfMaps.get(cityObj.state).add(cityObj.cityName); 
}


Thanks
AnupPrakash_AlgoworksAnupPrakash_Algoworks
Line 1 : Checkin if the iterated Citiy's Country is there in the Map
Line 2:  Getting the List of Cities Corresponding to the State and then adding the City to the List.
Line 3: Nothing ;)

Please Mark as best Answer it it Solves your query.
Absar Ahmad 6Absar Ahmad 6
I have the dummy object of Map<String,Map<String,List<String>>>() mapOf to acess this by visual force repeat tag 
--> 
My Controller Class -->
public class MapOfMap {
    public Map<String,Map<String,List<String>>> mapOf{get;set;}
    Public MapOfMap(){
        mapOf =new  Map<String,Map<String,List<String>>>();
        
        Map<String,List<String>> map1 = new Map<String,List<String>>();
        String[] str1 = new String[]{'mango','pineapple','apple','gavava'};
        String str1Id = 'fruits';
        map1.put(str1Id, str1);
         
        String[] str2 = new String[]{'abcd','efgh','ijkl','mnop','qrst','uvwx'};
        String str2Id = 'alpha';
        map1.put(str2Id, str2);
        
        mapOf.put('val1', map1);
        
        /*   Second Entry   */
        
        Map<String,List<String>> map2 = new Map<String,List<String>>();
        
        String[] str3 = new String[]{'1234','4567','6789','1234'};
        String str3Id = 'num';
        map2.put(str3Id, str3); 
        
        
        String[] str4 = new String[]{'up','new-delhi','hriyana','lucknow'};
        String str4Id = 'state';
        map2.put(str4Id, str4);
        
        String[] str5 = new String[]{'Virat','Dhoni','Yuraj','Shami','Pandya'};
        String str5Id = 'Team';
        map2.put(str5Id, str5);
        
        mapOf.put('val2', map2);
    }
}

My Visual force page for accessing map -->


<apex:page  controller="MapOfMap">
    <apex:repeat value="{!mapOf}" var="key1">  <!--Outer map key = key1 -->
        <apex:pageBlock title="{!key1}">
            <apex:repeat value="{!mapOf[key1]}" var="key2">  <!--Inner map key = key2 -->
                <apex:pageBlockSection title="{!key2}">
                    <apex:outputText value = "{!mapOf[key1][key2]}" />
                </apex:pageBlockSection>
            </apex:repeat>
        </apex:pageBlock>
    </apex:repeat>
</apex:page>

User-added image
Uttam KavitkarUttam Kavitkar
List<TestCitiesWrapper> cities = new List<TestCitiesWrapper>();    
cities.add(new TestCitiesWrapper('Mesa','Arizona','US'));
cities.add(new TestCitiesWrapper('Phoenix','Arizona','US'));
cities.add(new TestCitiesWrapper('Miami','Florida','US'));
cities.add(new TestCitiesWrapper('Patna','Bihar','IN'));
cities.add(new TestCitiesWrapper('Samastipur','Bihar','IN'));
cities.add(new TestCitiesWrapper('Brisbane','Queensland','AU'));
cities.add(new TestCitiesWrapper('Caboolture','Queensland','AU'));
cities.add(new TestCitiesWrapper('Airdrie','Alberta','CA'));
cities.add(new TestCitiesWrapper('Camrose','Alberta','CA'));
        
Map<String,Map<String,List<String>>> mapOfMaps = new Map<String,Map<String,List<String>>>();
for(TestCitiesWrapper cityObj : cities){
    if(mapOfMaps.containsKey(cityObj.country)){
        if(mapOfMaps.get(cityObj.country).containsKey(cityObj.state)){
             mapOfMaps.get(cityObj.country).get(cityObj.state).add(cityObj.cityName);
        }else{
            mapOfMaps.get(cityObj.country).put(cityObj.state, new List<String>{ cityObj.cityName });
        }
    }
    else{
        List<String> cities = new List<String>();
        cities.add(cityObj.cityName);
        Map<String,List<String>> mapStateCity = new Map<String,List<String>>();
        mapStateCity.put(cityObj.state,cities);
        mapOfMaps.put(cityObj.country,mapStateCity);
    }
}
system.debug('mapOfMaps==' + mapOfMaps);

public Class TestCitiesWrapper{
    
    public String cityName{ get; set; }
    public  String state{ get; set; }
    public String country{ get; set; }
    
    public TestCitiesWrapper(String cityName, String state, String country){
        this.cityName = cityName;
        this.state = state;
        this.country = country;
    }
}

This is properly working..Map withing map of list.. Please Mark as best Answer it it Solves your query.
 
Rajavardhan reddy MarikantiRajavardhan reddy Marikanti
list<opportunity> oppList=[Select id,name,Account.Name, Owner.Name, AccountId from Opportunity];
        Map<String,Map<String,List<opportunity>>> mapwithOppOwnerName = new Map<String,Map<String,List<opportunity>>>();
        for(Opportunity  opp : oppList){
            if(mapwithOppOwnerName.containsKey(opp.Owner.Name)){
                if(mapwithOppOwnerName.get(opp.Owner.Name).containsKey(opp.Account.Name)){
                    mapwithOppOwnerName.get(opp.Owner.Name).get(opp.Account.Name).add(opp);
                }else{
                    mapwithOppOwnerName.get(opp.owner.name).put(opp.Account.Name, new List<Opportunity>{ opp});
                }
            }
            else{
                List<Opportunity> oppNewList = new List<Opportunity>();
                oppNewList.add(opp);
                Map<String,List<opportunity>> newMap = new Map<String,List<opportunity>>();
                newMap.put(opp.Account.Name,oppNewList);
                System.debug('stateToCities-->'+newMap);
                mapwithOppOwnerName.put(opp.Owner.Name,newMap);
            }
        }