2

I have the following Integer list

List<Integer> arrayList = new ArrayList<Integer>();
   for (int i = 0; i < 7; i++) {
    arrayList.add(i);
}

So the list is like this [0,1,2,3,4,5,6]. My scenario is

If I give the value = 5 as parameter then I would like to split 5 sub list like this

[0,5], [1,6] , [2], [3], [4]

If I give the value = 4 as parameter then I would like to split 4 sub list like this

[0,4], [1,5], [2,6] , [3]

If I give the value = 3 as parameter then I would like to split 3 sub list like this

[0,3,6], [1,4], [2,5]

I already tested with below function but it is not my need.

public List<List<Integer>> chopped(List<Integer> list, final int splitCount) {
        List<List<Integer>> parts = new ArrayList<List<Integer>>();
        final int N = list.size();
        for (int i = 0; i < N; i += splitCount) {
            parts.add(new ArrayList<Notification>(list.subList(i, Math.min(N, i + splitCount))));
        }
        return parts;
    }

At the above function, I give splitCount to 5 then the function returns

[0,1,2,3,4], [5,6]

The result I expect is [0,5], [1,6] , [2], [3], [4]

3
  • Could you please specify the rule that you want to put into place? Instead of just giving examples?I thought I figured your requirement - but then I saw the N=3 example. Commented Aug 21, 2017 at 8:18
  • @GhostCat Sorry i do not understand what you said? Which parts don't you understand in my question. Sorry my English is not good. Commented Aug 21, 2017 at 8:24
  • I am saying: do not describe your requirement via examples only. You should also describe the requirement directly. I do not understand how to use "list-length" and "split factor" to determine the expected result. Commented Aug 21, 2017 at 8:27

3 Answers 3

6

How about:

public List<List<Integer>> chopped(List<Integer> list, final int splitCount) {
    List<List<Integer>> parts = new ArrayList<>(splitCount);
    for (int i = 0; i < splitCount; ++i) {
        parts.add(new ArrayList<>());
    }
    final int N = list.size();
    for (int i = 0; i < N; ++i) {
        parts.get(i % splitCount).add(list.get(i));
    }
    return parts;
}
Sign up to request clarification or add additional context in comments.

1 Comment

You probably want to add an explanation that the whole topic is about doing some sort of "module" distribution of the elements. And: I like your answer - it is simple and easy to understand!
0

Simple Solution Hope This Helps

    ArrayList<Integer> al1 = new ArrayList<>();
    ArrayList<ArrayList<Integer>> al2 = new ArrayList<>();
    // Add objects in  al1 ...
    int numberToSplit = 2;
    if (numberToSplit > al1.size()) {
        throw new Exception("Your message");

    } else {
        ArrayList<Integer> newArr;
        int counter = 0;
        for (int j = 0; j < numberToSplit; j++) {
            newArr = new ArrayList<>();
            for (int k = counter; k < al1.size(); k = k + numberToSplit) {
                newArr.add(ALI.get(k));

            }

            al2.add(newArr);
            counter++;

        }
    }

Comments

0

A functional implementation using Streams without variables:

public static void main(String[] args) {
    List<Integer> asList = Arrays.asList(0, 1, 2, 3, 4, 5, 6);
    System.out.println(chopped(asList, 3));
}

private static List<List<Integer>> chopped(List<Integer> source, int splitCount) {
    return IntStream.range(0, splitCount)
        .boxed()
        .map(index -> getElements(source, index, splitCount))
        .collect(Collectors.toList());
}

private static List<Integer> getElements(List<Integer> source, int index, int splitCount) {
    return IntStream.range(0, source.size())
            .filter(i -> i % splitCount  == index)
            .map(i -> source.get(i))
            .boxed()
            .collect(Collectors.toList());
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.