简体   繁体   中英

process data into batches of 1000

I have a java method with integer batch size as an input to my method.

I have to do processing in this method in batches of 1000 at a time. How could I split this batch size in batches of 1000 so that I also do not miss the reminder. So for example

  1. if batch size is 200 - I process 200 at one time.
  2. if batch size is 2000 - I process 1000 two times.
  3. if batch size is 4200 - I process 1000 four times and then rest of 200 at the end.

Please advise.

You haven't given much information about the input, so I'll assume that it's an Array of objects, and you want to process in chunks of a given batch size, and then then remainder. The input object type and how they get processed is up to you, because you didn't really specify how that's supposed to be handled.

It's not immediately clear to me what problem you're trying to solve, because there's no opportunity in the program the way you've specified it to do anything other than process chunks - it doesn't seem any more or less efficient or advantageous than processing the whole array at once.

void doBatch(SomeObject[] processMe, int batchSize) {
    int batchStartPtr;
    for( batchStartPtr = 0; batchStartPtr < processMe.length()-batchSize; batchStartPtr+=batchSize) {
        SomeObject[] toProcess = new SomeObject[batchSize];
        System.arraycopy(processMe, batchStartPtr, toProcess, 0, batchSize);
        process(toProcess); 
    }
    SomeObject[] toProcess = new SomeObject[processMe.length() - batchStartPtr];
    System.arraycopy(processMe, batchStartPtr, toProcess, 0, toProcess.length()];
    process(toProcess);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM