Apex Loops, The Simple Things

18 Sep

For loop over a map’s keys (KeySet)

map MyAwesomeMap;
//... other code
for (String KeyStr: MyAwesomeMap.keySet()){
  system.debug(KeyStr);
}

For loop over a map’s values

map MyAwesomeMap;
//... other code
for (CustomObject__c AnObject: MyAwesomeMap.values()){
  system.debug(AnObject);
}

For loop over a list

list MyAwesomeList;
//... other code
for (CustomObject__c AnObject: MyAwesomeList){
  system.debug(AnObject);
}

For loop over a set

set MyAwesomeSet;
//... other code
for (CustomObject__c AnObject: MyAwesomeSet){
  system.debug(AnObject);
}