Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

269
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

0

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

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

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda