A static Java method for multi-indexing a collection I have this small utility for selecting from a collection via multiple indices at one method invocation:

MultiIndexUtils.java

package net.coderodde.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

public final class MultiIndexUtils {

    private MultiIndexUtils() {}

    public static &lt;T&gt; List&lt;T&gt; multiIndex(Collection&lt;T&gt; collection,
                                         Collection&lt;Integer&gt; indices) {
        List&lt;T&gt; indexedElements = new ArrayList&lt;&gt;(indices.size());
        T[] array = (T[]) collection.toArray();
        indices.forEach((Integer index) -&gt; { 
            indexedElements.add(array[index]);
        });
        return indexedElements;
    }

    public static &lt;T&gt; List&lt;T&gt; multiIndex(Collection&lt;T&gt; collection,
                                         Integer... indices) {
        return multiIndex(collection, Arrays.asList(indices));
    }

    public static void main(String[] args) {
        List&lt;String&gt; strings = Arrays.asList("A", "B", "C", "D", "E", "F");
        System.out.println(multiIndex(strings, 1, 0, 3, 2, 0, 5, 4));
    }
}


Critique request

I would like to hear anything that comes to mind.
First up, nice touch with BADCAFE :)

Then, for my two cents:


To me it feels a bit strange using Collection for the element supplier type. Since we are indexing values, it would seem strange to use it on a Set for example, as it does not impose order.
On a similar note, using a Collection for the indices parameter seems strange as well, for the same reasons. A Set also implies uniqueness, so no two equal indices would be passed. 


Both would seem to make more sense being Lists to me. But this is something I am just saying based on what I am feeling :)

Continuing:


You can relax the type bound on the input parameter of the elements supplier, like so: 

public static &lt;T&gt; List&lt;T&gt; multiIndex(final Collection&lt;? extends T&gt; collection, ...


Now you can pass a collection parameterized with T or with a subtype of T, which makes it more versatile.
Seeing as you are extracting elements, you can annotate the array cast with a SuppressWarnings, as it is safe:

@SuppressWarnings("unchecked")
final T[] array = (T[]) collection.toArray();

Finally, I'd generally think twice before using forEach on a Stream, as it implies using stateful operations. Personally, I think in this case it can be more cleanely written as:

@SuppressWarnings("unchecked")
T[] array = (T[]) collection.toArray();
return indices.stream()
        .map(index -&gt; array[index])
        .collect(Collectors.toList());