チャールズXuriguera:
私は、私は、Javaにリファクタリングしたいとコードのこの部分を持っている8
List<String> menus = new ArrayList<String>();
for (Menu menu : resto1.getMenu()) {
MainIngredient mainIngredient = MainIngredient.getMainIngredient(menu.getName());
if (mainIngredient.getIngredient().indexOf("Vegan")!=-1) {
menus.add(menu.getName());
}
}
それはあまりにも多くのコードのように思えるこの単純なループをリファクタリングした後...私は正しくCompletableFuturesを使用していますか?
ExecutorService executorService = Executors.newCachedThreadPool();
List<CompletableFuture<MainIngredient>> priceFutureList = resto1.getMenu().stream()
.map(menu -> CompletableFuture.supplyAsync(
() -> MainIngredient.getMainIngredient(menu.getName()), executorService))
.collect(Collectors.toList());
CompletableFuture<Void> allFuturesDone = CompletableFuture.allOf(
priceFutureList.toArray(new CompletableFuture[priceFutureList.size()]));
CompletableFuture<List<MainIngredient>> priceListFuture =
allFuturesDone.thenApply(v -> priceFutureList.stream()
.map(CompletableFuture::join)
.collect(toList()));
Ousmane D .:
なぜだけではなく?
List<String> menus = resto1.getMenu()
.stream()
.map(m -> MainIngredient.getMainIngredient(m.getName()))
.filter(m -> m.getIngredient().indexOf("Vegan")!=-1)
.collect(toCollection(ArrayList::new));
あなたの不可欠なアプローチは、あなたが使用する必要があることは本当に遅いですかCompletableFuture
?