Stream

A collection of 1 posts

A Java geek
  • Me
  • Books
  • Speaking
  • Mentions
  • Focus
Feb 21, 2021 Functional Programming API Optional Stream

Optional.stream()

This week, I learned about a nifty 'new' feature of Optional that I want to share in this post. It’s available since Java 9, so its novelty is relative. Let’s start with the following sequence to compute the total price of an order: public BigDecimal getOrderPrice(Long orderId) { List<OrderLine> lines = orderRepository.findByOrderId(orderId); BigDecimal price = BigDecimal.ZERO; (1) for (OrderLine line : lines) { price = price.add(line.getPrice());

Nicolas Fränkel
A Java geek © 2008-2021
v. 0d7601da46da8643d2807330d7519ed353417aea/1077774977
Latest Posts