@OldCurmudgeon には優れた基本的なアプローチがありますが、より深刻なコードでは、おそらくCallable
キーの高価な処理を行い、 new を返すを作成する必要がありますCollection
。Executor や CompletionService と組み合わせることができます。最後に並行コレクションも必要ありません。
たとえば、キーが文字列の場合
public class DoesExpensiveProcessing implements Callable<Set<String>> {
final Set<String> inKeys;
public DoesExpensiveProcessing(Set<String> keys) {
this.inKeys = keys; // make a defensive copy if required...
}
public Set<String> call() {
// do expensive processing on inKeys and returns a Set of Strings
}
}
この時点では、並列コレクションも必要ありません
List<DoesExpensiveProcessing> doInParallel = new ArrayList<DoesExpensiveProcessing>();
for (Map map : maps) {
doInParallel.add(new DoesExpensiveProcessing(map.keySet()));
}
Set theResultingSet = new HashSet<String>();
List<Future<Set<String>>> futures = someExecutorService.invokeAll(doInParallel);
for (Future<Set<String>> f : futures) {
theResultingSet.addAll(f.get());
}