1

I am new to java. I have 5000 items in a list. I want to loop through the list so that I can get 100 items at a time. I have the following code:

   List<ProcessQueueBatch> processQueueBatchList = 
        Repository.getProcessQueue("Jor");
for (ProcessQueueBatch queueBatch : processQueueBatchList) 

   {
    // do some processing



   }

I want to do the processing of first 100 items in the list and then the next 100 items and then again next 100 items until all 5000 items are processed. I have total 5000 items in the list. How can I modify my for loop or list so that I can get 100 items at a time.

any help will be greatly appreciated.

2
  • A cycle of iteration only tackles one element at a time by default. Unless you want to do some weird list slicing, I'm not sure I see much value in this approach. Commented Aug 14, 2017 at 22:26
  • You can define an index variable an using if condition to help you seperate the loop. Commented Aug 14, 2017 at 22:28

2 Answers 2

0

You can use List.subList(int, int) to get the first K items from your processQueueBatchList like so:

int K = 100;
List<ProcessQueueBatch> newList = new ArrayList<>(processQueueBatchList.subList(0, K));

You should be able to take it from here...

Sign up to request clarification or add additional context in comments.

Comments

0

If your choice of List supports it, there's always List::subList.

There's also implementations using Streams. For example:

myList.stream()
        .skip(10) // Skip the first 'n' elements.
        .limit(100) // Limit the return size.
        .collect(Collectors.toList()); // Collect however you see fit.

Which would be helpful if you needed to do some filtering, as well.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.