Split Java ArrayList into equal parts
It seems like you are mixing up the number of segments and the number of elements per segment. In your example, both are the same, so the result is correct, but in other cases it will not be. For instance, i1 is created as the number of elements per segment, with the number of segments being hard-coded as 5.0. Then, in the loop, you treat i1 as the number of segments, while the number of elements per segment is hard-coded as 5. Mistakes like this will be easier to spot if you use properly named variables, like numSegments or segmentSize instead of i1 or integer constants.

Also:


no need to wrap the sublists into new ArrayList
you can just count by increments according to the number of elements per sublist instead of using two counters, for the current sublist and the offset
you could use a ternary ... ? ... : ... instead of that if/else


How about this:

public static void main(String[] args) {
    List&lt;String&gt; arrayList = new ArrayList&lt;String&gt;();
    for (int i = 0; i &lt; 23; i++) {
        arrayList.add(String.valueOf(i));
    }

    int size = 5;
    for (int start = 0; start &lt; arrayList.size(); start += size) {
        int end = Math.min(start + size, arrayList.size());
        List&lt;String&gt; sublist = arrayList.subList(start, end);
        System.out.println(sublist);
    }
}


Here, size is the number of elements in each sublist. So instead of counting the segment of the list and increasing a second counter by 5 each time, you can just increase the first counter by five in the third argument of the for loop. Also, you can use the result of subList directly, without passing it to a constructor first. And you can get rid of the if-else by using min to determine the end index.

Output:

[0, 1, 2, 3, 4]
[5, 6, 7, 8, 9]
[10, 11, 12, 13, 14]
[15, 16, 17, 18, 19]
[20, 21, 22]


If instead of sublists with 5 elements you wanted five sublists, then use this (same result for the given example, but different for other list sizes):

int size = (int) Math.ceil(arrayList.size() / 5.0);