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

268
Views
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 answers
Answer question

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 Report

0

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

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

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 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!