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

266
Visualizações
Java Lambda to convert an Object[] array to a subset Object[] array

I have an initial array, array1 which contains 30 elements and need to create a new array array2 containing elements 0-14 from array1.

I'm using IntStream as the mapper for the array index. But the following gives errors:

Object[] array2 = IntStream.range(0,14).map(x -> (Object)array1[x]).toArray(Object[]::new);

Error on (Object)array1[x]:

The type of the expression must be an array type but it resolved to List<Object[]>
over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

In your example, x is a type of integer. You should use the .mapToObj instead map method.

Object[] array2 = IntStream.range(0, 14)
        .mapToObj(x -> array1[x])
        .toArray(Object[]::new);
over 4 years ago · Santiago Trujillo Relatório

0

You can also use Arrays.copyOfRange() instead of Stream.

Object[] array2 = Arrays.copyOfRange(array1, 0, 14);
over 4 years ago · Santiago Trujillo Relatório

0

To get first 15 (0-14) elements from an array to an array

List<String> first15ElementsList = Arrays.stream(arr)
.limit(15)
.collect(Collectors.toList());
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