Apex Map

APEX Map

Example 1: Confirming Username Password combination from Login page and redirecting if the combination is valid for an employee
  1. public map<String,String> mapData = new map<String,String>();
  2. public Employee__c objE{get;set;}
  3. objE = new Employee__c();
  4. for(Employee__c objE : [select Email_Address__c, password__c FROM Employee__c]){
  5. mapData.put(objE.email_Address__c,objE.password__c);
  6. }
  7. if(mapData.get(objE.email_address__c) ==  objE.password__c){
  8. return (new pageReference('/apex/prdlistpage').setredirect(trur));
  9. }


Example 2:


  1. List<User> users = [SELECT Id, Name FROM User];
  2. Map<User,Decimal> userScores = new Map<User,Decimal>();
  3. for (integer i=1; i<users.size()+1; i++) {
  4.   userScores.put (users[i-1], i*100);
  5. }
  6. for (integer i=1; i<userScores.size()+1; i++) {
  7. System.debug('Score of User '+ users[i-1].Name +':' + userScores.get(users[i-1]));
  8. }

Example 3:


<apex:page controller="mapExample2Controller" >
<apex:form >
<apex:pageBlock >
<apex:pageblocktable value="{!userScores }" var="u">
<apex:column value="{!u.name}"/>
<apex:column value="{!userScores[u]}" headerValue="Score"/>
</apex:pageblocktable>
</apex:pageBlock>
</apex:form>
</apex:page>

public class mapExample2Controller {

    public Map<User,Decimal> getUserScores() {
        return userScores;
    }

    List<User> users{get;set;}
    Map<User,Decimal> userScores {get;set;}

public mapExample2Controller () {

    users = [SELECT Id, Name FROM User];
    userScores = new Map<User,Decimal>();
    for (integer i=1; i<users.size()+1; i++) {
      userScores.put (users[i-1], i*100);
    }
    for (integer i=1; i<userScores.size()+1; i++) {
    System.debug('Score of User '+ users[i-1].Name +':' + userScores.get(users[i-1]));
    }

}
}

Comments

Popular posts