Source:
<code><START> public ReadCountCollection subsetTargets(final Set<Target> targetsToKeep) { <END> Utils.nonNull(targetsToKeep, "the input target set cannot be null"); if (targetsToKeep.isEmpty()) { throw new IllegalArgumentException("the input target subset size must be greater than 0"); } if (targetsToKeep.size() == targets.size()) { if (targets.stream().anyMatch(name -> !targetsToKeep.contains(name))) { throw unknownTargetsToKeep(targetsToKeep); } else { return new ReadCountCollection(targets, columnNames, counts.copy()); } } else if (targetsToKeep.size() > targets.size()) { throw unknownTargetsToKeep(targetsToKeep); } final int[] targetsToKeepIndices = new int[targetsToKeep.size()]; int nextIndex = 0; final List<Target> resultTargets = new ArrayList<>(targetsToKeep.size()); for (int i = 0; i < targets.size(); i++) { final Target target = targets.get(i); if (!targetsToKeep.contains(target)) { continue; } else if (nextIndex >= targetsToKeepIndices.length) { throw unknownTargetsToKeep(targetsToKeep); } else { targetsToKeepIndices[nextIndex++] = i; resultTargets.add(target); } } if (nextIndex < targetsToKeep.size()) { throw unknownTargetsToKeep(targetsToKeep); } final double[][] resultCounts = new double[targetsToKeepIndices.length][columnNames.size()]; for (int i = 0; i < resultCounts.length; i++) { resultCounts[i] = counts.getRow(targetsToKeepIndices[i]); } return new ReadCountCollection(Collections.unmodifiableList(resultTargets), columnNames, new Array2DRowRealMatrix(resultCounts)); }</code><technical_language>targetsToKeep equally a Collection<Target>. (You change signature of unknownTargetsToKeep accordingly). _edit_ I need a Set get contains(name) in logarithmic time. more this. _edit edit_ Done thinking, a Set</technical_language>

Target:
public ReadCountCollection subsetTargets(final Set<Target> targetsToKeep) { Utils.nonNull(targetsToKeep, "the input target set cannot be null"); if (targetsToKeep.isEmpty()) { throw new IllegalArgumentException("the input target subset size must be greater than 0"); } if (targetsToKeep.size() > targets.size()) { throw unknownTargetsToKeep(targetsToKeep); } if (targetsToKeep.size() == targets.size()) { if (targets.stream().anyMatch(name -> !targetsToKeep.contains(name))) { throw unknownTargetsToKeep(targetsToKeep); } else { return new ReadCountCollection(targets, columnNames, counts.copy()); } } final int[] targetsToKeepIndices = new int[targetsToKeep.size()]; int nextIndex = 0; final List<Target> resultTargets = new ArrayList<>(targetsToKeep.size()); for (int i = 0; i < targets.size(); i++) { final Target target = targets.get(i); if (!targetsToKeep.contains(target)) { continue; } if (nextIndex >= targetsToKeepIndices.length) { throw unknownTargetsToKeep(targetsToKeep); } else { targetsToKeepIndices[nextIndex++] = i; resultTargets.add(target); } } if (nextIndex < targetsToKeep.size()) { throw unknownTargetsToKeep(targetsToKeep); } final double[][] resultCounts = new double[targetsToKeepIndices.length][columnNames.size()]; for (int i = 0; i < resultCounts.length; i++) { resultCounts[i] = counts.getRow(targetsToKeepIndices[i]); } return new ReadCountCollection(Collections.unmodifiableList(resultTargets), columnNames, new Array2DRowRealMatrix(resultCounts)); }