[SalesForce] how to add an object records to Map

I have an custom object called MapTesting and this object contains 10 records. I created one apex class. In apex class i created one Map like following.

public class Example {

    public Map<string, MapTesting__c> mp {set;get;}
     public void method(String mail){


      }
}

Now i wanna add MapTesting records to Map.
Can any one give solution?

Thanking You
KS Kumar

Best Answer

You can do this

public class Example {

 public Map<string, MapTesting__c> mp {set;get;}
 public void method(String mail){
     mp = new Map<string, MapTesting__c>();
     //1st way
     for(MapTesting__c mt : [SELECT FieldName FROM MapTesting__c Where Condition]){
          mp.put(Id, mt);   
    }
    //2nd way 
    mp = new Map<string, MapTesting__c>([SELECT FieldName FROM MapTesting__c Where Condition]);

 }
}
Related Topic