-1

I have a map as below:

Map<Integer, List<String>> iMap = new Map<Integer, List<String>>{
    1 => new List<String>{'a','b','c'},
    2 => new List<String>{'d','e','f'}        
};

What I am trying is to combine the 2 list values for above map which if done as below outputs as (a,b,c,d,e,f)

List<String> combinedList = new List<String>();
combinedList.addAll(iMap.get(1));
combinedList.addAll(iMap.get(2));

but I am looking to out put above list as (a,d,b,e,c,f) {one value from each key of the map}

I tried to do as shown below which didnt work and obviously doing same as above. Looking for suggestions.

Integer totalCount = combinedList.size();
List<String> returnList = new String[totalCount];

for(Integer i = 0; i < totalCount; i++){
    /*1*/if(math.mod(i+1, 2) != 0) returnList.set(i, combinedList.get(i));
    /*2*/else returnList.set(i, combinedList.get(i));
}

system.debug(returnList);

*** Map defined above will have lists of equal size (though solution for unequal size will be more ideal for me)

6
  • And what if the lists are not of the same size?
    – Adrian Larson
    Jul 13, 2017 at 15:05
  • thats another tricky thing.. some times, ya as u said map can contain lists of unequal sizes
    – sf_user
    Jul 13, 2017 at 15:06
  • Outside of guessing, this question is not answerable without knowing your requirements...please edit your post to clarify what needs to happen in this edge case.
    – Adrian Larson
    Jul 13, 2017 at 15:07
  • I can make the 2 lists in the map equal size.. edited the question to reflect same
    – sf_user
    Jul 13, 2017 at 15:09
  • @sf_user So your function will only ever deal with lists of the same size? Jul 13, 2017 at 15:09

2 Answers 2

4

I would say you should take these steps:

  1. Get the maximum length of all lists.
  2. Generate a sorted list of map keys.
  3. Iterate through integer indices up to this maximum.
  4. Iterate through your ordered keys and pull the value if your index is valid.

Roughly:

public static List<String> interleave(Map<Integer, List<String>> input)
{
    List<String> output = new List<String>();

    // Step 1
    Integer maxSize = 0;
    for (List<String> value : input.values())
    {
        if (value.size() > maxSize)
        {
            maxSize = value.size();
        }
    }

    // Step 2
    List<Integer> sortedKeys = new List<Integer>(input.keySet());
    sortedKeys.sort();

    for (Integer i = 0; i < maxSize; i++)
    { // Step 3
        for (Integer key : sortedKeys)
        { // Step 4
            List<String> value = input.get(key);
            if (value.size() > i)
            {
                output.add(value[i]);
            }
        }
    }
    return output;
}

And a couple input/output checks:

Input

Map<Integer, List<String>> input = new Map<Integer, List<String>>{
    1 => new List<String>{'a','b','c'},
    2 => new List<String>{'d','e','f'}
};
system.debug(interleave(input));

Output

(a, d, b, e, c, f)

Input

Map<Integer, List<String>> input = new Map<Integer, List<String>>{
    1 => new List<String>{'a','b','c'},
    2 => new List<String>{'w', 'x','y','z'},
    4 => new List<String>{'n', 'o'}
};
system.debug(interleave(input));

Output

(a, w, n, b, x, o, c, y, z)

0
1

If you lists are of equal size following should do a job

Map<Integer, List<String>> iMap = new Map<Integer, List<String>>{
    1 => new List<String>{'a','b','c'},
    2 => new List<String>{'d','e','f'}        
};


List<String> returnList = new List<String> ();


List<String> List1=iMap.get(1);
List<String> List2=iMap.get(2);

Integer totalCount = List1.size();

if (totalCount == List2.size())
{
    for(Integer i = 0; i < totalCount; i++){
        returnList.add(List1.get(i));
        returnList.add(List2.get(i));
    }

    system.debug(returnList);
}
else
{
    //thow exception
    //or return null
}
0

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.