How to split an arraylist in multiple arraylists of k lenght in Java ? I don't want a view of the new lists, but I want to create them

Natacha BK :

So, I've read a lot about how to create an Arraylist from multimple arraylists, but not the opposite. I found some codes to have a VIEW of some arraylists based on a single arraylist, but I do not need the view. I need to CREATE new arraylists list (of lenght 9) from an arraylist (of lenght 81). The thing is that I need to have the possibility to manage each arraylist separately.

static ArrayList<Integer> nums = new ArrayList<Integer>();

> Output : 
[0, 0, 1, 0, 1, 2, 0, 2, 3, 0, 3, 4, 0, 4, 5, 0, 5, 6, 0,
    > 6, 7, 0, 7, 8, 0, 8, 9, 1, 0, 2, 1, 1, 3, 1, 2, 4, 1, 3, 5, 1, 4, 6,
    > 1, 5, 7, 1, 6, 8, 1, 7, 9, 1, 8, 1, 2, 0, 3, 2, 1, 4, 2, 2, 5, 2, 3,
    > 6, 2, 4, 7, 2, 5, 8, 2, 6, 9, 2, 7, 1, 2, 8, 2, 3, 0, 4, 3, 1, 5, 3,
    > 2, 6, 3, 3, 7, 3, 4, 8, 3, 5, 9, 3, 6, 1, 3, 7, 2, 3, 8, 3, 4, 0, 5,
    > 4, 1, 6, 4, 2, 7, 4, 3, 8, 4, 4, 9, 4, 5, 1, 4, 6, 2, 4, 7, 3, 4, 8,
    > 4, 5, 0, 6, 5, 1, 7, 5, 2, 8, 5, 3, 9, 5, 4, 1, 5, 5, 2, 5, 6, 3, 5,
    > 7, 4, 5, 8, 5, 6, 0, 7, 6, 1, 8, 6, 2, 9, 6, 3, 1, 6, 4, 2, 6, 5, 3,
    > 6, 6, 4, 6, 7, 5, 6, 8, 6, 7, 0, 8, 7, 1, 9, 7, 2, 1, 7, 3, 2, 7, 4,
    > 3, 7, 5, 4, 7, 6, 5, 7, 7, 6, 7, 8, 7, 8, 0, 9, 8, 1, 1, 8, 2, 2, 8,
    > 3, 3, 8, 4, 4, 8, 5, 5, 8, 6, 6, 8, 7, 7, 8, 8, 8]

I found this code :

List<Integer> bigList = nums
List<List<Integer>> smallerLists = Lists.partition(bigList, 10);

But for some reason it doesn't work as I can't import the lists.partition() method in Guava Library

Arvind Kumar Avinash :

You can do it as follows:

import java.util.Collection;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

public class Main {
    public static void main(String[] args) {
        List<Integer> bigList = List.of(0, 0, 1, 0, 1, 2, 0, 2, 3, 0, 3, 4, 0, 4, 5, 0, 5, 6, 0, 6, 7, 0, 7, 8, 0, 8, 9,
                1, 0, 2, 1, 1, 3, 1, 2, 4, 1, 3, 5, 1, 4, 6, 1, 5, 7, 1, 6, 8, 1, 7, 9, 1, 8, 1, 2, 0, 3, 2, 1, 4, 2, 2,
                5, 2, 3, 6, 2, 4, 7, 2, 5, 8, 2, 6, 9, 2, 7, 1, 2, 8, 2, 3, 0, 4, 3, 1, 5, 3, 2, 6, 3, 3, 7, 3, 4, 8, 3,
                5, 9, 3, 6, 1, 3, 7, 2, 3, 8, 3, 4, 0, 5, 4, 1, 6, 4, 2, 7, 4, 3, 8, 4, 4, 9, 4, 5, 1, 4, 6, 2, 4, 7, 3,
                4, 8, 4, 5, 0, 6, 5, 1, 7, 5, 2, 8, 5, 3, 9, 5, 4, 1, 5, 5, 2, 5, 6, 3, 5, 7, 4, 5, 8, 5, 6, 0, 7, 6, 1,
                8, 6, 2, 9, 6, 3, 1, 6, 4, 2, 6, 5, 3, 6, 6, 4, 6, 7, 5, 6, 8, 6, 7, 0, 8, 7, 1, 9, 7, 2, 1, 7, 3, 2, 7,
                4, 3, 7, 5, 4, 7, 6, 5, 7, 7, 6, 7, 8, 7, 8, 0, 9, 8, 1, 1, 8, 2, 2, 8, 3, 3, 8, 4, 4, 8, 5, 5, 8, 6, 6,
                8, 7, 7, 8, 8, 8);

        final AtomicInteger counter = new AtomicInteger(0);
        Collection<List<Integer>> smallerLists = bigList.stream()
                .collect(Collectors.groupingBy(i -> counter.getAndIncrement() / 9)).values();

        // Display the smaller lists
        smallerLists.stream().forEach(System.out::println);
    }
}

Output:

[0, 0, 1, 0, 1, 2, 0, 2, 3]
[0, 3, 4, 0, 4, 5, 0, 5, 6]
[0, 6, 7, 0, 7, 8, 0, 8, 9]
[1, 0, 2, 1, 1, 3, 1, 2, 4]
[1, 3, 5, 1, 4, 6, 1, 5, 7]
[1, 6, 8, 1, 7, 9, 1, 8, 1]
[2, 0, 3, 2, 1, 4, 2, 2, 5]
[2, 3, 6, 2, 4, 7, 2, 5, 8]
[2, 6, 9, 2, 7, 1, 2, 8, 2]
[3, 0, 4, 3, 1, 5, 3, 2, 6]
[3, 3, 7, 3, 4, 8, 3, 5, 9]
[3, 6, 1, 3, 7, 2, 3, 8, 3]
[4, 0, 5, 4, 1, 6, 4, 2, 7]
[4, 3, 8, 4, 4, 9, 4, 5, 1]
[4, 6, 2, 4, 7, 3, 4, 8, 4]
[5, 0, 6, 5, 1, 7, 5, 2, 8]
[5, 3, 9, 5, 4, 1, 5, 5, 2]
[5, 6, 3, 5, 7, 4, 5, 8, 5]
[6, 0, 7, 6, 1, 8, 6, 2, 9]
[6, 3, 1, 6, 4, 2, 6, 5, 3]
[6, 6, 4, 6, 7, 5, 6, 8, 6]
[7, 0, 8, 7, 1, 9, 7, 2, 1]
[7, 3, 2, 7, 4, 3, 7, 5, 4]
[7, 6, 5, 7, 7, 6, 7, 8, 7]
[8, 0, 9, 8, 1, 1, 8, 2, 2]
[8, 3, 3, 8, 4, 4, 8, 5, 5]
[8, 6, 6, 8, 7, 7, 8, 8, 8]

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=358134&siteId=1