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
rajubalajirajubalaji 

how to write map and list with country and languages

Hi everyone,

Can anyone please guide my how to write a map<string> and list<string> together.please find below code and help me on this issue.

Map<String, Map<String, List<String>>> countryMap = new Map<String, Map<String, List<String>>> {'Germany' => 'German',  'Spain' => 'Spanish' ,  'France' => 'French' ,  'UnitedStates' => 'English' , 'UnitedStates' => 'Spanish' ,  'Canada' => 'English' , 'Canada' => 'French'};
            for(String c : newCountries){
                for(String l : newLanguages){
                    if(countrymap.containskey(c) && !Countrymap.get(c).contains(l)){
                        error = true;
                        account.adderror( 'country ' + c + ' does not support language ' + l);
                        return;
                    }
                }
            }

Thanks inadvance,
P.Balu
Best Answer chosen by rajubalaji
rajubalajirajubalaji
Hi everyone,

For Slecting single language for single country it was working fine.

public class testCode {
    public static void validateCode(){
        List<String> Countries = new List<String>();
        List<String> Languages = new List<String>();
        Countries.add('UnitedStates');
        Languages.add('Telugu');
        Map<String, List<String>> countryMap = new Map<String, List<String>>{
            'Germany' => new List<String>{'German'},
                'Spain' => new List<String>{'Spanish'},
                    'France' => new List<String>{'French'},
                        'UnitedStates' => new List<String>{'English', 'Spanish'},
                            'Canada' => new List<String>{'English', 'French'}};
                                for(String c : Countries){
                                    for(String l : Languages){
                                        if(countryMap.containskey(c) && !countryMap.get(c).contains(l)){
                                            system.debug( 'Country ' + c + ' does not support language ' + l);
                                        }
                                    }
                                }
    }
}

Thanks For everyone for helping foe this.
 

All Answers

Jessica RiffeJessica Riffe
Map<String, List<String>> countryMap = new Map<String, List<String>>{
    'Germany' => new List<String>{'German'},  
    'Spain' => new List<String>{'Spanish'},
    'France' => new List<String>{'French'},
    'UnitedStates' => new List<String>{'English', 'Spanish'},
    'Canada' => new List<String>{'English', 'French'}};
I believe this is the code you are looking for to populate your countryMap.
 
Deepali KulshresthaDeepali Kulshrestha
Hi balaji,

I have gone through your problem please refer below process:
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'));    
    Map<String,Map<String,List<String>>> mapOfMaps = new Map<String,Map<String,List<String>>>();
    for(City  cityObj : Cities){
    
        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);
        }
    }


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com
rajubalajirajubalaji
Hi Deepali,

Thanks You so much for reply.For cites and statesit will be fine but writing for countires and language it was very diffcult to comparing.

public class testCode {
    public static void validateCode(){
        List<String> Countries = new List<String>();
        List<String> Languages = new List<String>();
        Map<String, List<String>> countryMap = new Map<String, List<String>>{
            'Germany' => new List<String>{'German'},
                'Spain' => new List<String>{'Spanish'},
                    'France' => new List<String>{'French'},
                        'UnitedStates' => new List<String>{'English', 'Spanish'},
                            'Canada' => new List<String>{'English', 'French'}};
        Countries.add('Germany');
        Languages.add('German');
        for(String c : Countries){
            for(String l : Languages){
                if(countrymap.containskey(c) && !c.equals(l)){
                    system.debug( 'country ' + c + ' does not support language ' + l);
                    return;
                }
            }
        }
    }
}

but here when i am using debug it was showing germany does not support language german ike that.so please check and update on this.

Thanks in advance,
p.Balaji 
rajubalajirajubalaji
Hi everyone,

For Slecting single language for single country it was working fine.

public class testCode {
    public static void validateCode(){
        List<String> Countries = new List<String>();
        List<String> Languages = new List<String>();
        Countries.add('UnitedStates');
        Languages.add('Telugu');
        Map<String, List<String>> countryMap = new Map<String, List<String>>{
            'Germany' => new List<String>{'German'},
                'Spain' => new List<String>{'Spanish'},
                    'France' => new List<String>{'French'},
                        'UnitedStates' => new List<String>{'English', 'Spanish'},
                            'Canada' => new List<String>{'English', 'French'}};
                                for(String c : Countries){
                                    for(String l : Languages){
                                        if(countryMap.containskey(c) && !countryMap.get(c).contains(l)){
                                            system.debug( 'Country ' + c + ' does not support language ' + l);
                                        }
                                    }
                                }
    }
}

Thanks For everyone for helping foe this.
 
This was selected as the best answer