Here
is the sample code to access Set and List values via loop salesforce,
Accessing
List
To
access List of values through for loop it’s simple, the below examples show
access list of element via loop.
- List<Integer> myList = new List<Integer>{1,2,3};
- for(integer i=0;i<myList.size();i++)
- {
- System.debug(myList[i]);
- }
Accessing
Set
To
access Set values through for loop it’s different than accessing List, the
below examples show access set of element via loop.
Set<integer> mySet = new
Set<integer>{1, 2, 3};
- for(integer i=0;i<mySet.size();i++)
- {
- System.debug((new list<Integer>(mySet))[i]);
- }
Accessing
Map
To
access Map values it’s similar to set.
Accessing
Key
The
below examples show access KeySet of Map via loop.
- Map<string, string> colorCodes = new Map<String, String>();
- colorCodes.put('Red', 'FF0000');
- colorCodes.put('Blue', '0000A0');
- for(integer i=0;i<colorCodes.size();i++)
- {
- System.debug((new list<String>(colorCodes.keySet())[i]));
- }
Accessing Values
The
below examples shows access Values of Map via loop.
- Map<string, string> colorCodes = new Map<String, String>();
- colorCodes.put('Red', 'FF0000');
- colorCodes.put('Blue', '0000A0');
- for(integer i=0;i<colorCodes.size();i++)
- {
- System.debug((new list<String>(colorCodes.keySet())[i]));
- }