1

How can refactor this with 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);
}
Dara
  • 19
  • 1

1 Answers1

2

Stream over the products, convert each product to a LineItem and collect to a List.

List<SessionCreateParams.LineItem> elements =
    experience.getProducts()
              .stream()
              .map(p -> stripeProxy.createLineItem(1L, p.getPriceId(), 
                                                   getTaxRate(p.getPriceId())))
              .collect(Collectors.toList());
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
Eran
  • 387,369
  • 54
  • 702
  • 768