Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

196
Views
Partition list to sub lists in Java8

I'm wondering if there is any nice a way using Java 8 to do something like this:

int amount = 1000;
int firstIndex = 0;
int lastIndex = firstIndex + amount;
List<int> projectIdsList;

while (lastIndex < projectIdsList.size()){
        doSomethingWhitSubList(projectIdsList.subList(firstIndex, lastIndex));
        firstIndex = lastIndex;
        lastIndex = firstIndex + amount;
}
doSomethingWhitSubList(projectIdsList.subList(firstIndex,  projectIdsList.size());
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You can partition the list into sublist using IntStream, and I'm not really sure about your intention after partition, but you can collect the sublist's into a list using collect

List<List<Integer>> subLists = IntStream.range(0,integers.size())
            .filter(i->i%amount == 0)
            .mapToObj(i->integers.subList(i,i+amount > integers.size() ? integers.size() : i+amount))
            .collect(Collectors.toList());

Or you can also use forEach for any operations after partitioning

IntStream.range(0,integers.size())
            .filter(i->i%amount == 0)
            .mapToObj(i->integers.subList(i,i+amount > integers.size() ? integers.size() : i+amount))
            .forEach(this::doSomethingWhitSubList);
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!