I have an arrayList of N objects containing a few fields like so:
[
{
"firstName": "Todd",
"lastName": "Jones"
},
{
"firstName": "Mike",
"lastName": "Johnson"
},
{
"firstName": "Tina",
"lastName": "Jones"
},
{
"firstName": "Tommy",
"lastName": "Wiseau"
},
....
]
I am trying to split a processing job into equal(ish) parts based on a passed in index of what chunk I'm currently on and how many total chunks there are.
I currently have some logic I've started that tries to do this, but I think where I am failing is that my math doesn't account for not being equally divisible. What is the best way to make sure I am always getting the correct indices in my subList?
(Example: 9 name objects, want part 1 of 2 equally split arrays)
This is my current logic that isn't working:
final int namesCount = names.size();
final int namesDividedCount = namesCount / numOfChunks;
int startIndex, endIndex;
if (chunkIndex == 1) {
startIndex = 0;
endIndex = namesDividedCount;
} else if (chunkIndex == numOfChunks) {
startIndex = namesCount - namesDividedCount;
endIndex = namesCount;
} else {
startIndex = ((chunkIndex - 1) * namesDividedCount) + 1;
endIndex = chunkIndex * namesDividedCount;
}
return names.subList(startIndex, endIndex);