-3

I am having the list . List has 28 records. I need to split the records in the list into 13 + 13 + 2 in the code. Is it possible?. If it is possible how to do that? Can anyone help me with this?

Thanks in Advance.

  • 4
    Could you please share what you have already tried? Just as an idea you need a while loop which will have a counter from 0 to 13 which will drop down to setup and indicate a stay of a new list – nchursin Apr 7 at 7:39
  • 3
    I'm glad to see that you've received a high quality answer, but please bear in mind that Stack Exchange expects you to share what you've tried and where you're stuck. Particularly for basic logic, we need you to make an attempt to implement before asking here. See How to Ask. – David Reed Apr 7 at 13:55
3

you can do it by means of the while loop and checking the current size of the partition. Here is a class, that allows a splitting list of any list type.

public with sharing class Lists {

    public static List<List<Object>> doPartions(List<Object> records, Integer size, Type destType){
        checkNotNull(records);
        checkArgument(size > 0);
        checkNotNull(destType);
        return new Partition(records, size, destType).doPartition();
    }

    private static void checkNotNull(Object reference) {
        if (reference == null) {
            throw new NullPointerException();
        }
    }

    private static void checkArgument(Boolean expression) {
        if (!expression) {
            throw new IllegalArgumentException();
        }
    }

    private class Partition{
        private List<Object> items;
        private Integer splitSize;
        private Type destType;

        Partition(List<Object> records, Integer size, Type destType){
            this.items = records;
            this.splitSize = size;
            this.destType = destType;
        }

        List<List<Object>> doPartition(){
            List<List<Object>> result = (List<List<Object>>)destType.newInstance();
            List<Object> protoList = items.clone();
            protoList.clear();
            List<Object> tempList = protoList.clone();
            Integer index = 0, count = 0, size = items.size();
            while(index < size) {
                tempList.add(items.get(index++));
                ++count;
                if(count == splitSize) {
                    result.add(tempList);
                    tempList = protoList.clone();
                    count = 0;
                }
            }
            if(!tempList.isEmpty()) {
                result.add(tempList);
            }
            return result;
        }
    }

    public class IllegalArgumentException extends Exception {}
}

Usage of it

List<Integer> input = new List<Integer> {1, 2, 3, 4, 5, 6, 7};
List<List<Integer>> result = (List<List<Integer>>) Lists.doPartions(input, 2, List<List<Integer>>.class);
System.debug(result);

DEBUG|((1, 2), (3, 4), (5, 6), (7))

| improve this answer | |

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