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
Get multiple elements from list by indices in constant time

What is the best way to get multiple elements from a list by their indices in constant time?

If I've an array:

List<String> list = new ArrayList<>();
list.add("a");
list.add("b");
list.add("c");
list.add("d");
list.add("e");

And I've an list/array with indices:

List<Integer> indices = new ArrayList<>();
indices.add(0);
indices.add(2);
indices.add(3);

How can I get a,c,d in constant time? I need something like this:

List<String> filtered = list.filterByIndex(indices);
filtered.stream().forEach(x -> System.out.print(x));
// output:"acd"

UPDATE: The printing of the items doesn't have to be in constant time of course, only the collecting of items. The code above of printing the elements is just for demonstrating purposes only.

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

I suggest:

    List<String> filtered = indices.stream()
            .map(list::get)
            .collect(Collectors.toList());

The result is the desired:

[a, c, d]

Assuming the list has constant-time access (as an ArrayList has), this runs in time that is linear in the number of elements requested (length of indices), but does not increase with the length of the list list. As has been discussed in comments, this is the best we can do.

Edit: Honestly I don’t know whether the collecting step above is in linear time in the number of collected elements. Possibly extensions of list capacity cost time, and probably this doesn’t take more than linear time. If we need to be sure, we need to collect this way instead:

            .collect(Collectors.toCollection(() -> new ArrayList<>(indices.size())));

This makes sure a list with appropriate capacity is allocated from the outset so no extensions will be needed.

over 4 years ago · Santiago Trujillo Report

0

To create a list:

List<String> filtered = new ArrayList<>();
indices.forEach(index -> filtered.add(list.get(index)));

System.out.println(filtered);

Stream and map solution

List<String> filtered = indices.stream()
        .map(index -> list.get(index))
        .collect(Collectors.toList());

If you need only string you can do it with StringBuilder

StringBuilder sb = new StringBuffer();
indices.forEach(index -> sb.append(list.get(index)));

System.out.println(sb.toString());
over 4 years ago · Santiago Trujillo Report

0

You might do something like this:

IntStream.range(0, list.size())
       .boxed()
       .filter(indices::contains)
       .map(list::get)
       .forEach(System.out::println);
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!