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 <T> List<T> multiIndex(Collection<T> collection, Collection<Integer> indices) { List<T> indexedElements = new ArrayList<>(indices.size()); T[] array = (T[]) collection.toArray(); indices.forEach((Integer index) -> { indexedElements.add(array[index]); }); return indexedElements; } public static <T> List<T> multiIndex(Collection<T> collection, Integer... indices) { return multiIndex(collection, Arrays.asList(indices)); } public static void main(String[] args) { List<String> 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.