How can I split an ArrayList into several lists?

20,429

Solution 1

google-collections has Lists.partition(). You supply the size for each sublist.

Solution 2

To start, you may find List#subList() useful. Here's a basic example:

public static void main(String... args) {
    List<Integer> list = new ArrayList<Integer>();
    list.add(0);
    list.add(1);
    list.add(2);
    list.add(3);
    list.add(4);
    list.add(5);
    list.add(6);

    int targetSize = 3;
    List<List<Integer>> lists = split(list, targetSize);
    System.out.println(lists); // [[0, 1, 2], [3, 4, 5], [6]]
}

public static <T extends Object> List<List<T>> split(List<T> list, int targetSize) {
    List<List<T>> lists = new ArrayList<List<T>>();
    for (int i = 0; i < list.size(); i += targetSize) {
        lists.add(list.subList(i, Math.min(i + targetSize, list.size())));
    }
    return lists;
}

Note that I didn't use the splittedInto as it doesn't make much sense in combination with targetSize.

Share:
20,429
Arthur Ronald
Author by

Arthur Ronald

IT analyst Feel free to contact me at arthurseveral [at] gmail [dot] com

Updated on August 14, 2020

Comments

  • Arthur Ronald
    Arthur Ronald almost 4 years

    See the following ArrayList:

    List<Integer> values = new ArrayList<Integer>();
    values.add(0);
    values.add(1);
    values.add(2);
    values.add(3);
    values.add(4);
    values.add(5);
    values.add(6);
    

    So we have:

    integerList.size(); // outputs 7 elements
    

    I need to show a Google chart as follows:

    http://chart.apis.google.com/chart?chs=300x200&chd=t:60,-1,80,60,70,35&cht=bvg&chbh=20,4,20&chco=4C5F2B,BED730,323C19&chxt=y&chxr=0,0,500

    To generate its values, I just call

    StringUtils.join(values, ","); // outputs 0,1,2,3,4,5,6
    

    It happens it supports up to 1000 pixel width. So if I have many values, I need to split my ArrayList into other ArrayLists to generate other charts. Something like:

    Integer targetSize = 3; // And suppose each target ArrayList has size equal to 3
    
    // ANSWER GOES HERE
    List<List<Integer>> output = SomeHelper.split(values, targetSize);
    

    What Helper should I use to get my goal?

  • jdmichal
    jdmichal over 14 years
    Nitpick: Should probably provide targetSize to the ArrayList constructor, so that it allocates the proper size array off the bat and doesn't overallocate / resize later.
  • BalusC
    BalusC over 14 years
    No, the targetSize is not the length of the lists. It would have been the splittedInto which I didn't use. The subList already takes this into account.
  • BalusC
    BalusC over 14 years
    new ArrayList<List<T>>((list.size() / targetSize) + 1) would have been better.
  • Arthur Ronald
    Arthur Ronald over 14 years
    I think people like reinvent the wheel
  • Arthur Ronald
    Arthur Ronald over 14 years
    In fact, you are right. It does not make sense splittedInto. (+1)
  • jdmichal
    jdmichal over 14 years
    Oh, right. I completely read that code wrong. Thought the list was being made for the sub-list. Thanks for the corrections. As pointed out, the correct size can still be determined.
  • jdmichal
    jdmichal over 14 years
    No. The problem is that in many cases it's harder to find the wheel than to invent your own.
  • jdmichal
    jdmichal over 14 years
    BTW, even that linked class duplicates functionality. Lists.asList(...) duplicates java.util.Arrays.asList(). And there's several newXXX methods that would function exactly the same if there was a space after the new, thereby invoking constructors instead.
  • Arthur Ronald
    Arthur Ronald over 14 years
    @jdmichal it's harder to find the wheel than to invent your own (+1) for your comment
  • Kevin Bourrillion
    Kevin Bourrillion over 14 years
    jdmichal: not true in either case. 'List<TypeOfObject> list = Lists.newArrayList()' can be used in place of 'List<TypeOfObject> list = new ArrayList<TypeOfObject>()'. Many users strongly prefer to avoid the redundancy. Lists.asList() combines either an element and array, or two elements and an array, into a single list. If you read the documentation, you'll see that this is so, and why it's so. Arrays.asList() doesn't do that. What got you so interested in trying to discredit our library as being reinventions of wheels?
  • tjg184
    tjg184 about 11 years
    Be careful on the targetSize here. A 0 will cause an infinite loop. :)