簡體   English   中英

如何用 Stream 重構它?

[英]How to refactor this with Stream?

如何用 java stream 重構這個?

List<SessionCreateParams.LineItem> elements = new ArrayList<>();
SessionCreateParams.LineItem lineItem;

for (Product product : experience.getProducts()) {
    String taxRate = getTaxRate(product.getPriceId());
    lineItem = stripeProxy.createLineItem(1L, product.getPriceId(), taxRate);
    elements.add(lineItem);
}

Stream 在產品上,將每個產品轉換為LineItem並收集到List

List<SessionCreateParams.LineItem> elements =
    experience.getProducts()
              .stream()
              .map(p -> stripeProxy.createLineItem(1L, p.getPriceId(), 
                                                   getTaxRate(p.getPriceId())))
              .collect(Collectors.toList());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM