Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

200
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda