[英]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.