2

How to iterate over this map on individual record without much of for loop?

Map<String,Map<String,List<Map<String,String>>>> nested map = 
   new Map<String,Map<String,List<Map<String,String>>>>();
  • 8
    why you have created this type of structure then? – Santanu Boral Jan 24 '17 at 17:22
  • such a datastructure will be extremely hard to follow the code and would benefit from using inner classes – cropredy Jan 24 '17 at 17:28
  • 1
    For loops are the correct way to iterate through anything. That's what they're designed to do. – sfdcfox Jan 24 '17 at 17:55
  • Maybe something has gotten lost in translation here, when you say without much of for loop do you mean without putting for loops in for loops in for loops in for loops? Or do you mean without using for loops at all? – Dan Jones Jan 24 '17 at 17:57
  • Why the aversion to nested loops? The term 'iterate over' generally entails using a loop of some flavor. If you store all of the keys for a map in local variables, you could technically do without a loop. However, such a solution would result in very fragile (and very much longer) code. If you don't take cropredy's suggestion about using inner classes, nested for loops would be the right tool for the job here. – Derek F Jan 24 '17 at 18:03
3

Whenever I have a generic structure that starts to get unwieldy I move it into a dedicated class.

If you need to iterate over a number of members in the collection often you can implement Iterable<X> on the dedicated class.

public class MyConvolutedDataStructure implements Iterator<String>
{
    private Map<String,Map<String,List<Map<String,String>>>> nestedMap = 
        new Map<String,Map<String,List<Map<String,String>>>>();

    // You will need to maintain member variables to track the current iterator progress.

    public boolean hasNext() {
        // Are there more records to iterate?
        return false;
    }

    public String next() {
        // return the next value from the inner dictionary

        return null;
    }
}

You could potentially simplify the process by also maintain a parallel list within the dedicated class of the values you want to iterate over.

The code that consumes this would then appear as:

MyConvolutedDataStructure toLoopOver = new MyConvolutedDataStructure();
// populate the data structure within toLoopOver...

for(String innerString : toLoopOver) {
    // Use the deeply nested string without a care for the ugly structure
}
| improve this answer | |
4
for (Map<String,List<Map<String,String>>> i : foo.values()) {
    for (List<Map<String,String>> j : i.values()) {
        for (Map<String,String> k : j) {
            for (String val : k.values()) {
                System.debug(val);
            }
        }
    }
}

But you will go to hell for this code.

| improve this answer | |
  • Upvote for going to hell. – akcorp2003 Jan 24 '17 at 18:30

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.