Devrath
Devrath

Reputation: 42824

Grouping of collection in java

I Have a collection of integers(This can be a dynamic data source)

ArrayList<Int> data = [1,2,3,4,5,6,7]

What I am able to do

I can generate Equal data set like [1,2,3][4,5,6] [7] using the answer from here


Question: How can I give a limit to the number of groups like max 2 so that resulting output is [1,2,3,4][5,6,7]or [1,2,3][4,5,6,7]

Upvotes: 0

Views: 92

Answers (3)

vcmkrtchyan
vcmkrtchyan

Reputation: 2626

Instead of using a third party library, you can just iterate over the list, divide the items into subgroups until you reach the limit, and add the rest of the elements to the last group

private static List<List<Integer>> partition(List<Integer> list, int groupSize, int limit) {
    List<List<Integer>> result = new ArrayList<>();

    for (int i = 0; i < list.size(); i++) {
        int groupIndex = i / groupSize;
        if(groupIndex > limit - 1) {
            groupIndex = limit - 1;
        }

        if(groupIndex >= result.size()) {
            result.add(new ArrayList<>());
        }
        result.get(groupIndex).add(list.get(i));
    }

    return result;
}

Upvotes: 1

Md Golam Rahman Tushar
Md Golam Rahman Tushar

Reputation: 2375

Here the logic is if the size of the list is divisible by maximum partition number then by just partitioning the list to the Quotient value sizes solve the problem. But if not divisible then the extra values(reminder) are added to the first partition and the remaining list size is divisible by the maximum partition number and can be partitioned to Quotient sizes.

public static List<List<Integer>> performePartition(List<Integer> list, int mx) {
    if(list.size() % mx == 0) {
        return Lists.partition(list, list.size() / mx);
    }

    int reminder = list.size() % mx;
    int div = list.size() / mx;
    List<Integer> firstList = list.subList(0, div + reminder);

    List<List<Integer>> ret = new ArrayList<>();
    ret.add(firstList);
    List<Integer> remainingList = list.subList(div + reminder, list.size());
    ret.addAll(Lists.partition(remainingList, div));

    return ret;
}

Upvotes: 0

WJS
WJS

Reputation: 40044

Here is what I came up with. Not certain how you wanted to divide up the "stragglers". Note that since sublists are just a view of the original list, they must be passed into a List constructor to be independent of the original list.

        List<Integer> list =
            new ArrayList<>(List.of(1, 2, 3, 4, 5, 6, 7, 8, 9));

        List<List<Integer>> lists = new ArrayList<>();
        int groupSize = 2;
        int ngroups = 6;
        for (int i = 0; i < ngroups && !list.isEmpty(); i++) {

            if (groupSize > list.size()) {
                groupSize = list.size();
            }
            List<Integer> sublist = new ArrayList<>(list.subList(0, groupSize));
            list = list.subList(groupSize,list.size());

            lists.add(sublist);

        }

        System.out.println(lists);

Upvotes: 0

Related Questions