public List<Output> getOutputFromKeys(final Collection<String> keys) { final List<Output> outputs = Lists.newArrayList()
This for-loop could be heavy depends on how long "getOutput" method takes. If the method takes lots of time, we will need improve the performance of that.
To improve the performance, we can take in advance the ExecutorService class of the Java.
public List<Output> getOutputFromKeys(final Collection<String> keys) { final int threads = Runtime.getRuntime().availableProcessors(); final ExecutorService service = Executors.newFixedThreadPool(threads);
class LoadOutput implements Callable<Output> { private String key;
public LoadOutput(final String key) { this.key = key; } @Override
public Output call() throws Exception { final Output out = getOutput(key); if (out == null) { logger.warn(String.format("Output with key %s is null", key)); } return out; } } final List<Future<Output>> futures = Lists.newArrayList(); for (final String key : keys) { final LoadOutput callable = new LoadOutput(key); futures.add(service.submit(callable)); } service.shutdown(); return Lists.newArrayList(Collections2.transform(futures, new Function<Future<Output>, Output>() { @Nullable
@Override
public Output apply(Future
The improvement use multi-threading to improve the for-loop and use callback methods. That can help us to improve our performance.