views:

810

answers:

2

Hi,

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&amp;chd=t:60,-1,80,60,70,35&amp;cht=bvg&amp;chbh=20,4,20&amp;chco=4C5F2B,BED730,323C19&amp;chxt=y&amp;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?

+5  A: 

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.

BalusC
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.
jdmichal
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
`new ArrayList<List<T>>((list.size() / targetSize) + 1)` would have been better.
BalusC
In fact, you are right. It does not make sense splittedInto. (+1)
Arthur Ronald F D Garcia
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
+8  A: 

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

Kevin Bourrillion
I think people like reinvent the wheel
Arthur Ronald F D Garcia
No. The problem is that in many cases it's harder to find the wheel than to invent your own.
jdmichal
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.
jdmichal
@jdmichal it's harder to find the wheel than to invent your own (+1) for your comment
Arthur Ronald F D Garcia
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?
Kevin Bourrillion