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
M SreekanthM Sreekanth 

See The Below Class And My requirement is How to get based on place 'SRnagar' i want get city name how to retrive That

Scenario:- Create a Map with cityName as key,and its corresponing
======    places as values

Public class Map_Practice{
public static void mapPrac(){
    //*Create a Map with cityName as key,and its corresponing places as values
    Map<string,list<string>> myMap = new Map<string,list<string>>();
        list<string> Hplaces = new list<string>{'SRnagar','LBnagar','KBHP'};
        list<string> Bplaces = new list<string>{'Marthali','Ecity','Whitefield'};
        list<string> Cplaces = new list<string>{'Thambaram','ChromePet','TNnagar'};
            myMap.put('HYD',Hplaces);
               myMap.put('BAN',Bplaces);
            myMap.put('CHE',Cplaces);
        system.debug('City and Its Places'+myMap);
        //Get all the city names
        set<string> keys =myMap.keySet();
         system.debug('Cityes==>'+keys);
        //get all the places
        /* *if my map of values is list<string> then we can get all the values list<list<string>>*/
        list<list<string>> values = myMap.values();
           system.debug('Places==>'+values);  
        } 

}

Please Help Me i am new to salesforce
Best Answer chosen by M Sreekanth
Amidou CisseAmidou Cisse
In the given code, you can get the city name for a place by looping through the values in the map myMap and checking if the place is present in any of the lists.

Here's an example:

typescript
Copy code
public class Map_Practice{
  public static void mapPrac(){
    Map<string,list<string>> myMap = new Map<string,list<string>>();
    list<string> Hplaces = new list<string>{'SRnagar','LBnagar','KBHP'};
    list<string> Bplaces = new list<string>{'Marthali','Ecity','Whitefield'};
    list<string> Cplaces = new list<string>{'Thambaram','ChromePet','TNnagar'};
    myMap.put('HYD',Hplaces);
    myMap.put('BAN',Bplaces);
    myMap.put('CHE',Cplaces);
    // Place you want to find city name for
    string place = 'SRnagar';
    string cityName = null;
    for (string key : myMap.keySet()) {
      if (myMap.get(key).contains(place)) {
        cityName = key;
        break;
      }
    }
    System.debug('City name for place ' + place + ' is ' + cityName);
  }
}

This code loops through all the keys in the map myMap and checks if the place 'SRnagar' is present in the list of places for each key. If the place is found, the city name is assigned to cityName and the loop breaks. The final city name is printed in the debug log.

All Answers

SOURAV GUHA 9SOURAV GUHA 9
Map<string,list<string>> myMap = new Map<string,list<string>>();

    list<string> Hplaces = new list<string>{'SRnagar','LBnagar','KBHP'};
    list<string> Bplaces = new list<string>{'Marthali','Ecity','Whitefield'};
    list<string> Cplaces = new list<string>{'Thambaram','ChromePet','TNnagar'};
    myMap.put('HYD',Hplaces);
    myMap.put('BAN',Bplaces);
    myMap.put('CHE',Cplaces);
    //system.debug('City and Its Places'+myMap);

// Add this code block
List <string> places =  new list<string>();
string cityName;
for (string s:myMap.keyset() )   //iterating over all the keys of the map
{
    places= myMap.get(s);   //for each key I am getting the list of places inside places list
    for (string str: places )    //iterating over places list
    {
        if (str=='SRnagar')
        {
            cityName=s;
        }
    }
   
}

system.debug(cityName);

Please check this code, mark it as best if it resolves your issue. Thanks
Amidou CisseAmidou Cisse
In the given code, you can get the city name for a place by looping through the values in the map myMap and checking if the place is present in any of the lists.

Here's an example:

typescript
Copy code
public class Map_Practice{
  public static void mapPrac(){
    Map<string,list<string>> myMap = new Map<string,list<string>>();
    list<string> Hplaces = new list<string>{'SRnagar','LBnagar','KBHP'};
    list<string> Bplaces = new list<string>{'Marthali','Ecity','Whitefield'};
    list<string> Cplaces = new list<string>{'Thambaram','ChromePet','TNnagar'};
    myMap.put('HYD',Hplaces);
    myMap.put('BAN',Bplaces);
    myMap.put('CHE',Cplaces);
    // Place you want to find city name for
    string place = 'SRnagar';
    string cityName = null;
    for (string key : myMap.keySet()) {
      if (myMap.get(key).contains(place)) {
        cityName = key;
        break;
      }
    }
    System.debug('City name for place ' + place + ' is ' + cityName);
  }
}

This code loops through all the keys in the map myMap and checks if the place 'SRnagar' is present in the list of places for each key. If the place is found, the city name is assigned to cityName and the loop breaks. The final city name is printed in the debug log.
This was selected as the best answer
M SreekanthM Sreekanth
I tried as same as above but it's throwing error Method signature incorrect please check it then how to write it let me know please


public class Student_Exten {
    public static void MapPractice2(){
        Student s = new student();
        s.Name='Sunil';
        s.phone='912831281';
        s.age=15;
        s.rollNo=123;
        system.debug('S Data ==> '+s);
        Student s1 = new student();
        s1.Name='karishma';
        s1.phone='9128312341';
        s1.age=14;
        s1.rollNo=124;
        system.debug('S1 Data ==> '+s1);
        
        Student s2 = new student();
        s2.Name='Sailu';
        s2.phone='9128317371';
        s2.age=13;
        s2.rollNo=125;
        system.debug('S2 Data ==> '+s2);
        
        list<student> sList = new list<student>{s,s1,s2};
        system.debug('List Of All Students==> '+sList);
        //Create a Map Roll No as Key And Student As Values
        Map<integer,student> sMap = new map<integer,student>();
        //Add key and values into map through list
        for(student stu:sList){
           sMap.put(stu.rollNo,stu);
        }
        system.debug('sMap Key And Values ==> '+sMap);
        //Add all The keys into set
        set<integer> sKeys=sMap.keySet();
        system.debug('Set Of Keys==> '+sKeys);
        
        //Add all The Values into List
        list<student> sValues=sMap.values();
        system.debug('List Of Values==> '+sValues);
        //Get the values based on key
        list<student> stuList = new list<student>();
        for(integer k:sKeys){
          student studValues=sMap.get(k);
            if(studValues.rollNo==124){
                stuList.add(studValues);
            }
        }
        system.debug('Based on key retrived value is ==> '+stuList);
        //get the Key corresponding any values
        integer age =13;
        integer rNo=Null;
        for(integer k:sKeys){
            if(sMap.get(k).contains(age)){
                rNo=k;
                break;
            }
        }
    }
}