Hi, In this scenario we have shown how to use a map in Apex. This example shows, how to create a map with Account name as key and account as value…
Apex Class
public class mapexample {
public map<string,Account> accmap{set;get;}
public list<Account> accs{set;get;}
public list<string> names{set;get;}
public list<string> phones{set;get;}
public mapexample(){
accmap = new map<string,Account>();
Account a1 = new Account();
a1.Name = 'First';
a1.Phone = '9999999999';
accmap.put(a1.Name,a1);
Account a2 = new Account();
a2.Name = 'Second';
a2.Phone = '8888888888';
accmap.put(a2.Name,a2);
}
public void getData(){
names = new list<string>();
phones = new list<string>();
set<string> keys = accmap.keyset();
for(string s:keys){
Account a=accmap.get(s);
}
}
}
VisualForce Page
<apex:page controller="mapexample">
<apex:pageBlock title="Map Example">
<apex:pageBlockTable value="{!accmap}" var="a">
<apex:column value="{!accmap[a].Name}" headerValue="Account Name" />
<apex:column value="{!accmap[a].Phone}" headerValue="Account Phone" />
</apex:pageBlockTable>
</apex:pageBlock>
</apex:page>
Output