patternjavaMinor
Partitioning a list to a list of smaller lists
Viewed 0 times
listsmallerpartitioninglists
Problem
I created a utility method
Unit tests:
```
public class ListUtilsTest {
private List> partition(Iterable iterable, int size) {
return ListUtils.partition(iterable, size);
}
@Test
public void testPartitionEmpty() {
assertEquals(Arrays.>asList(), partition(Arrays.asList(), 3));
}
@Test
public void testPartitionNull() {
assertEquals(Arrays.>asList(), partition(null, 3));
}
@Test
public void testPartitionZeroSize() {
List orig = Arrays.asList(1, 2, 3);
assertEquals(Arrays.asList(orig), partition(orig, 0));
}
@Test
public void testPartitionNegativeSize() {
List orig = Arrays.asList(1, 2, 3);
assertEquals(Arrays.asList(orig), partition(orig, -3));
}
@Test
public void testPartitionInts() {
assertEquals(
Arrays.asList(
Arrays.asList(1, 2),
Arrays.asList(3, 4),
Arrays.asList(5)),
partition(Arrays.asList(1, 2, 3, 4, 5), 2));
}
@Test
public void t
ListUtils.partition(iterable, size) to partition a potentially large list to a list of smaller lists:public class ListUtils {
public static List> partition(Iterable iterable, int size) {
if (iterable == null) {
return Collections.emptyList();
}
if (size list = new ArrayList<>();
for (T item : iterable) {
list.add(item);
}
return Arrays.asList(list);
}
List> result = new ArrayList<>();
List segment = new ArrayList<>(size);
int i = 0;
for (T item : iterable) {
segment.add(item);
if (++i == size) {
result.add(segment);
segment = new ArrayList<>(size);
i = 0;
}
}
if (i > 0) {
result.add(segment);
}
return result;
}
}Unit tests:
```
public class ListUtilsTest {
private List> partition(Iterable iterable, int size) {
return ListUtils.partition(iterable, size);
}
@Test
public void testPartitionEmpty() {
assertEquals(Arrays.>asList(), partition(Arrays.asList(), 3));
}
@Test
public void testPartitionNull() {
assertEquals(Arrays.>asList(), partition(null, 3));
}
@Test
public void testPartitionZeroSize() {
List orig = Arrays.asList(1, 2, 3);
assertEquals(Arrays.asList(orig), partition(orig, 0));
}
@Test
public void testPartitionNegativeSize() {
List orig = Arrays.asList(1, 2, 3);
assertEquals(Arrays.asList(orig), partition(orig, -3));
}
@Test
public void testPartitionInts() {
assertEquals(
Arrays.asList(
Arrays.asList(1, 2),
Arrays.asList(3, 4),
Arrays.asList(5)),
partition(Arrays.asList(1, 2, 3, 4, 5), 2));
}
@Test
public void t
Solution
I don't agree with your method of handling 0 or negative partition sizes.
A partition is always meant to be a sub-section of a whole. A partition with size 0 is impossible if the whole is non-empty, as there is no way to cut up a whole into sub-sections of size 0. (Note, an empty whole will of course be divisible into partitions of size 0). The same logic applies for negative values. I would think raising a
Now as for handling sub-section sizes larger than the size of your list, you already define that by having incomplete sub-sections if your sub-section size doesn't divide into your container size. You are missing a test for this, however.
A final remark: since
use this for large data sets.
A partition is always meant to be a sub-section of a whole. A partition with size 0 is impossible if the whole is non-empty, as there is no way to cut up a whole into sub-sections of size 0. (Note, an empty whole will of course be divisible into partitions of size 0). The same logic applies for negative values. I would think raising a
RuntimeException such as an IllegalArgumentException would make more sense in that context. null objects shouldn't be partition-able either, as they are not even empty "wholes".Now as for handling sub-section sizes larger than the size of your list, you already define that by having incomplete sub-sections if your sub-section size doesn't divide into your container size. You are missing a test for this, however.
A final remark: since
Iterable isn't confined to Collection's measly 2 billion item limit for indexing, you might consider using a long for your partition size if you plan to use this for large data sets.
Context
StackExchange Code Review Q#61162, answer score: 5
Revisions (0)
No revisions yet.