Issue
If I have items like this
-> [1,2,3,4,5,6,7,8,9,10]
And if ChunkSize =3, spring batch ItemReader reads item like
[1,2,3]
[4,5,6]
[7,8,9]
[10]
[1,2,3]
[4,5,6]
[7,8,9,10]
Is there any way to do this?
Solution
for (i in 1..12) { // added for testing
val list = IntArray(i) { it + 1 }.toList()
val result = list
.chunked(3)
.groupBy { it.count() }
.run {
values.flatten().run {
when (keys.size) {
1 -> this
else -> dropLast(2).plusElement(takeLast(2).flatten())
}
}
}
println(result)
}
Test output:
[[1]]
[[1, 2]]
[[1, 2, 3]]
[[1, 2, 3, 4]]
[[1, 2, 3, 4, 5]]
[[1, 2, 3], [4, 5, 6]]
[[1, 2, 3], [4, 5, 6, 7]]
[[1, 2, 3], [4, 5, 6, 7, 8]]
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
[[1, 2, 3], [4, 5, 6], [7, 8, 9, 10]]
[[1, 2, 3], [4, 5, 6], [7, 8, 9, 10, 11]]
[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
Answered By - lukas.j
Answer Checked By - Gilberto Lyons (JavaFixing Admin)