0

I'm trying to split an ArrayList into separate blocks. I have a function that returns table data collected in jsoupie, just as individual elements. I wonder how to make a second list, so that it consisted of every 5 subsequent elements of the first list.

   example:
    1 list:
    lista1.get (0) = Monday;
    lista1.get (1) = Tuesday;
    ...
    lista1.get (6) = Sunday;

    and here I would like to make it

                     //lista1.get(1) .. lista1.get(6)
    lista2.get (0) = Monday, Tuesday, Wednesday, Thursday, Friday;
    lista2.get (1) = any, other data contained in, this, leaves, stated below;

Thanks

3
  • Are you going to create a list of list? use subList on the first list and add to a new list Commented Sep 23, 2014 at 16:35
  • From you Question it's look like you should try list of list. Commented Sep 23, 2014 at 16:36
  • Yes, I would like to create list of every 5 elements head lists, but I get data from JSOUP table (website) , and I don't know how many datas is there, so I want to split this array by every 5 subsequent Commented Sep 23, 2014 at 16:40

4 Answers 4

1

I have written two methods for you. One splits your list into list of lists, another one groups nested lists into single string.

    public class LinkedListSplitByFive {

        private static List<List<String>> splitByGroupAsList(List<String> list, int elementsInGroup) {
            List<List<String>> result = new ArrayList<List<String>>();

            List<String> group = new ArrayList<String>(elementsInGroup);
            for (String s : list) {
                group.add(s);
                if (group.size() == elementsInGroup) {
                    result.add(group);
                    group = new ArrayList<String>(elementsInGroup);
                }
            }
            if (!group.isEmpty()) {
                result.add(group);
            }
            return result;
        }

        private static List<String> splitByGroupAsString(List<String> list, int elementsInGroup) {
            List<List<String>> lists = splitByGroupAsList(list, elementsInGroup);
            List<String> result = new ArrayList<String>(lists.size());

            for (List<String> group : lists) {
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < group.size(); i++) {
                    sb.append(group.get(i));
                    if (i == group.size() - 1) {
                        sb.append(";");
                    } else {
                        sb.append(", ");
                    }
                }
                result.add(sb.toString());
            }
            return result;
        }

        public static void main(String[] args) {

            List<String> list = Arrays.asList("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11");

            System.out.println(splitByGroupAsList(list, 5));
            // [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11]]

            System.out.println(splitByGroupAsString(list, 5));
            // [1, 2, 3, 4, 5;, 6, 7, 8, 9, 10;, 11;]
        }
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, it was so helpful ;)
1

Try something like this:

public static void main(String[] args) {

    List<String> flatList = Arrays.asList(
          "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11");

    List<List<String>> outer = new ArrayList<List<String>>();

    int cnt = 0;
    List<String> inner = new ArrayList<String>(5);

    for (String str : flatList) {
        inner.add(str);    // fill inner list

        if (++cnt == 5) {
            outer.add(inner);
            inner = new ArrayList<String>(5);
            cnt = 0;
        }
    }

    if (!inner.isEmpty()) {
        outer.add(inner);
    }

    System.out.println(outer);
}

Result of this code is:

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

2 Comments

But, can you explain me, what should I do when I want to return List<String>? Because now, I'm getting a List<List<String>
@Algeroth check my answer below. It answers this question too.
0

You can also try a map of Object* with List.
* Change Object for whatever you need.

Comments

0

Considering you need a list of lists as output, you can try this. I haven't tried compiling it, so there might be some syntax errors and typos. But the main idea is to keep picking 5 elements each time from the original list and adding those into new list. And keeping a listOfLists, which eventually contains all such new lists.

ArrayList<ArrayList<String>> listOfLists = new ArrayList<ArrayList<String>>();
    int size=list.length %5=0?i/5:i/5+1;
    for(int i=0;i<size;i++){
        listOfLists.add(Arrays.copyOfRange(list, i*5, i*5+5));
    }
# return listOfLists;

2 Comments

Considering you are using list.size(), list is the subclass of Collection. Then how can you pass it as argument to Arrays.copyOfRange which accepts only plain arrays?
I considered input list as an array. I should have used length attribute instead of size(), thanks for pointing it out. Even if the input is ArrayList, one can convert it into an array before passing it to Arrays.copyOfRange() method. Of corse, it might not be the most efficient way.

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.