1

i am unable to fetch all Keys and Value simultaneously using forEach loop..

 for (Map<String,String> key : parameters) {

        queryParameters = key.values();//?
    }
1
  • Naman is your issue resolved ?
    – Ratan Paul
    Jul 8 '16 at 6:05
2

If you want to fetch them all simultaneously, you have to do it outside the loop:

Map<String,String> aMap = new Map<String,String>();
Set<String> mapKeys = aMap.keySet();
List<String> mapValues = aMap.values();

Source: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_map.htm

If you absolutely need to all the keys inside a for loop (I don't know why... it sounds like bad practice to reference keys other than the one you're currently iterating), you can reference the mapKeys variable inside the for loop.

Or, just iterate and reference them one at a time as @MSCB described above

1
  • Yes. The map key values are a collection of set
    – MSCB
    Jul 7 '16 at 11:41
0
Map<String,String> myMap = new Map<String,String>();

//iterate over the map key 
for (String key : myMap.keySet()) {

    system.debug('=======map key====='+key);
    //get the value based on key 
    system.debug('=======map value====='+myMap.get(key));
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.