2

Just looking to see if anyone has ideas for this I have a method that splits a List into a List of Lists of close to even size.

public static List<List<Object>> splitList(List<Object> InputList,Integer Count){
    List<List<Object>> ObjectList = new List<List<Object>>();
    for(Integer i = 0;i < Count && i < InputList.size();i++){
        ObjectList.add(new List<Object>());
    }
    for(Integer i = 0;i < InputList.Size();i++){
        ObjectList[Math.mod(i, Count)].add(InputList[i]);
    }
    return ObjectList;
}

But when I return the list I can't unless I'm missing a trick here just cast it like below

List<List<String>> ObjectListList = (List<List<String>>) ApexHelper.splitList(test,3);

What would be the best way on converting this other than do multiple for loops like below?

List<List<Object>> ObjectListList = ApexHelper.splitList(test,3);
List<List<String>> StringListList = new List<List<String>>();
for(List<Object> ObjectList : ObjectListList){
    List<String> StringList = new List<String>();
    for(Object o : ObjectList){
        StringList.add((String) o);
    }
    StringListList.add(StringList);
}
4

Until Apex allows user-defined generic classes and methods, you can't really do this in a simple way. You could modify your method to actually return a List<List<T>> of a specified type which should allow it to be cast to that type after being returned, unfortunately there isn't a way to have a particular object return its own type so you have to have the user provide it:

public static List<List<Object>> splitList(List<Object> InputList,Integer Count, string listType){
    List<List<Object>> ObjectList = (List<List<Object>>)Type.ForName('List<List<' + listType + '>>').newInstance();
    Type innerListType = Type.ForName('List<' + listType + '>');
    for(Integer i = 0;i < Count && i < InputList.size();i++){
        ObjectList.add((List<Object>)innerListType.newInstance());
    }
    for(Integer i = 0;i < InputList.Size();i++){
        ObjectList[Math.mod(i, Count)].add(InputList[i]);
    }
    return ObjectList;
}
7
  • I can't seem to cast from this either there isn't any special casting method I need to do or is the below right? @IllusiveBrian List<List<String>> ObjectListList = (List<List<String>>) ApexHelper.splitList(test,3); May 8 at 16:15
  • @KarlLivesey My bad, didn't do the same thing to the lists being added to the main list. May 8 at 16:25
  • Yep Works I'm just glad I don't need to make a special splitter for each type of data or object I want to push through. May 8 at 16:40
  • I suppose the only alternative would be to pass in both lists in the input then clone them? May 9 at 9:32
  • @KarlLivesey I think that would work. You could also force a TypeException by casting the list to something else and parse the message to find the concrete type of the list. May 9 at 11:42

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.